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
EDIT2:
$with = (isset($_GET['with']) && !empty($_GET['with'])) ? '<iframe type="text/html" width="540" height="385" src=" 'frameborder="0">
</iframe>' : false;I tried this but didn't work.
02 Answers
Check if variable set and echo the code if it is
<?php if(isset($_GET['id'])):?> <iframe type="text/html" width="540" height="385" src=" echo $_GET['id']; ?>" frameborder="0"> </iframe>
<?php endif ?> 0 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'])) ? ' : 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 type="text/html" width="540" height="385" src=" '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.