A newbie question re php varibles inside an IF

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rcgauer
Forum Newbie
Posts: 2
Joined: Sun Jul 02, 2006 10:33 am

A newbie question re php varibles inside an IF

Post by rcgauer »

Hello....

I can use php to determine if a url variable exists and echo something to the screen based upon it:

<?php
if ($_GET["ID"] != '')
{ echo "there is no ID";
}
else {
echo "yes there is";
}
?>

but I can't figure a bowlegs and curly braces syntax that allows me to do further php stuff based on that knowledge. I would like, for example, to include a specific hyperlink or image based on the URL variable. These work on their own:

<?php print '<a href="printer_friendly.mvc?ID=' . $_GET["ID"] . '">' ;?>printer friendly</a>
<?php print '<IMG SRC="images/' . $_GET["ID"] . '.gif" >';?>

How can I stuff that sort of thing inside the conditional statement, so that the image or link only appears if the url variable exists?

Thanks
rg
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php

if (!empty($_GET['ID']) && is_numeric($_GET['ID']))
{ 
    print '<a href="printer_friendly.mvc?ID=' . $_GET['ID'] . '">printer friendly</a>';
    print '<IMG SRC="images/' . $_GET['ID'] . '.gif" >';
}

?>
Have a look at the isset() and empty() functions when checking if variables exist and or are empty or not. You should always check whether the data you are expecting is the proper type. Have a search for "input sanitation" and "XSS injection"
rcgauer
Forum Newbie
Posts: 2
Joined: Sun Jul 02, 2006 10:33 am

Post by rcgauer »

Thanks
Post Reply