Id variable comes as empty in the url string


<?php 
$id= $_GET['id'];
?>


<li ><a href="prueba.php?id=<?php  $id   ?>"><img src="images/menu.gif" align="middle" alt="menu" width="100&#37;" height="22" /></a></li>

the link in the li tags will come out with an empty id in the url string like

http://localhost/store/prueba.php?id=

Don’t know why is coming as empty.

<?php echo $id; ?>

Thank you very much Guido2004

even better

<?php 
$id= (int) $_GET['id'];//type casting
?>

so that you wont get any empty values, @ least 0

The “int” will force a value, but What about if not value is passed let’s suppose then what will be return?

Try this code:

$id = (int) $_GET['id'];
echo "prueba.php?id=".$id;

with empty $_GET[‘id’], $id will be equal to 0 due to type casting.

http://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting
http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

I’m actually surprised to see that converting from null to int is considered to be undefined behavior, even though it’s pretty widespread that people assume it should yield zero.