Determine if a variable has been defined.
Often times I have found myself trying to determine if a variable has been set or not – especially with php which is unforgiving if you try to use a variable that hasn’t been defined. It is for that reason that I stumbled on the isset method, which will allow you to determine if a variable has been set. If the isset function finds that a variable is not set then it will return false.
The code below would display the following:
myvar is set
myvar2 is not set
------------------------------------------------------------------------------------------------------------
$myvar = “hello”;
if(isset($myvar)){ echo “myvar is set”; } else { echo “myvar is not set”;}
echo “<br>”;
if(isset($myvar2)){ echo “myvar2 is set”; } else { echo “myvar2 is not set”;}