检查是否设置了GET,然后发布与PHP变量连接的HTML [关闭]

Ok, so I have an id passed from the previous page

$_GET['id'];

I want to check if the id has been set first, and if so, then echo the following code :

EDIT: please also look at http://pastebin.com/P2Jnapnf

EDIT2:

 $with = (isset($_GET['with']) && !empty($_GET['with'])) ? '<iframe class="youtube-player"
 type="text/html" width="540" height="385" src="http://www.youtube.com/embed/'.$_GET['with']. 'frameborder="0">
</iframe>' : false;

I tried this but didn't work.

Check if variable set and echo the code if it is

<?php if(isset($_GET['id'])):?>
    <iframe class="youtube-player" type="text/html" width="540" height="385"
       src="http://www.youtube.com/embed/<?php echo $_GET['id']; ?>" frameborder="0"> 
    </iframe>
<?php endif ?>

You can use the following - the isset will check if the variable is set (as in it exists) and !empty to ensure that the variable actually has data in it:

if(isset($_GET['id']) && !empty($_GET['id']))
{
    // You code here.
}

Edit: I would do something like this:

$myID = (isset($_GET['id']) && !empty($_GET['id'])) ? 'http://www.youtube.com/embed/'.$_GET['id'] : false;

echo ($myID) ? $myID : "";

The first part will do a ternary operator and if (isset($_GET['id']) && !empty($_GET['id'])) evaluates to true, it will assign $myID the value of your link and the ID from the GET. If not, it will assign a false boolean.

The next line uses another ternary and if $myID is false, outputs an empty string, otherwise it displays the full link.

Edit 2: Try this:

echo (isset($_GET['with']) && !empty($_GET['with'])) ? '<iframe class="youtube-player" type="text/html" width="540" height="385" src="http://www.youtube.com/embed/'.$_GET['with']. 'frameborder="0"></iframe>' : false;

Edit 3: A deceze correctly points out: The !empty function will by default call the isset function first - meaning that if it isn't set, it cannot pass a !empty function with anyting else than false.