Double check of variables.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

In some examples of a PHP implementation I saw the following:
if ( isset(x) and x==12)
I do not understand what for before to check whether some variable has
some value one need to check whether this variable is set. If the
second condition is true than the first one is also true, isn't?

Re: Double check of variables.

Quoted text here. Click to load it

The order of checking here is the same as in C, C++, Java, etc.  It checks  
the first condition.  If that fails, it does NOT check the second condition.  
If you were to check the second condtion only, then it might cause an error  
condition that produces undesirable results.  In the case you cite, that  
probably won't happen.  It is generally used best when you might have a  
pointer off into never-never land.  So, it is good programming practice to  
alway be CERTAIN that your variable being tested actually exists before  
testing its value.


Re: Double check of variables.

The reason for isset check is that without it, if x isn't set, PHP will
generate a warning on some systems.

Re: Double check of variables. wrote:
Quoted text here. Click to load it

Yes, but the problem occurs when the variable *isn't* set.

//$x = 12;
if ($x == 12) {
    echo 'The variable $x is equal to 12.';

The above code will give you the following:
"Notice: Undefined variable: x in [file] on line [line]"

It's not fatal, so the script will keep going, but it's generally not a  
good idea to just ignore things like that.

- Ryan

Site Timeline