CancelImage Upload

How to find out if a variable has already been set in PHP

When you have thousands and thousands of lines of code it is sometimes hard to tell if a variable has already been set. Of course most programmers will simply check if the string is equal to NULL, but this may not be sufficient all the time. A string can after all have been set, and then later on been given the value of NULL.

Here then is a sure way to check if currently a string has already been set or not by using the function isset(). Simply define the variable in question inside the brackets, and let PHP find out if the variable has already been defined:

<?php

$string = "Hello World";

if(isset($string))
{

       echo $string;

}

?>

The same of course will as mentioned also apply for an empty variable:

<?php

$string = "";

if(isset($string))
{

       echo "The string has been set!";

}

?>

Only in our last example nothing will be returned, since the variable $string has not been defined:

<?php

if(isset($string))
{

       echo "The string has been set!";

}

?>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com