So, I have some PHP code that looks a bit like this:
<body>
The ID is
<?php
echo $_GET["id"] . "!";
?>
</body>
Now, when I pass an ID like http://localhost/myphp.php?id=26
it works alright, but if there is no ID like just http://localhost/myphp.php
then it outputs:
The ID is
Notice: Undefined index: id in C:\xampp\htdocs\myphp.php on line 9
!
I have searched for a way to fix this but I cannot find any way to check if a URL variable exists. I know there must be a way though.
You can use
isset
function:You can create a handy function to return default value if index doesn't exist:
You can also try to validate it at the same time: