quick question about conditionals in PHP

bitt3n

Senior member
Dec 27, 2004
202
0
76
instead of evaluating whether a variable is set, and then evaluating its contents, like this

if (isset($var)) {
if ($var == 3) {
echo($var);
}
}

is it considered bad form to do this

if ( (isset($var)) && ($var==3) ) {
echo($var);
}

since it's obvious what's going on and this should never fail (since PHP stops evaluating the conditional if isset() returns false?
 

presidentender

Golden Member
Jan 23, 2008
1,166
0
76
Short-circuit evaluation is wonderful. I do the latter all the time; then again, I only have a year of paid PHP development under my belt.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: bitt3n
instead of evaluating whether a variable is set, and then evaluating its contents, like this

if (isset($var)) {
if ($var == 3) {
echo($var);
}
}

is it considered bad form to do this

if ( (isset($var)) && ($var==3) ) {
echo($var);
}

since it's obvious what's going on and this should never fail (since PHP stops evaluating the conditional if isset() returns false?

If the first condition is false, the second will not be evaluated. Plus the second code snippet is more readable. I always try to cut down on nested ifs as much as possible.
 

Hyperblaze

Lifer
May 31, 2001
10,027
1
81
I'd go as far to say that the first one would be used by a less experienced developer

second form is short and sweet. I like it
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
the 2nd one if thats all thats happening

normally there's an isset conditional, and inside there's a bunch of other value based conditionals or a switch statement
 

IEC

Elite Member
Super Moderator
Jun 10, 2004
14,600
6,084
136
I like #2 better. It's more readable and I hate nested ifs.
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: presidentender
Originally posted by: txrandom
or just do:

if ($var == 3) {
echo $var;
}

That squawks if $var isn't set.

not it doesnt.

of $var isnt set its assigned NULL so it just doesnt evaluate to 3.

so actually you can cut out the braces entirely and it would work as intended

if($var==3) echo $var;

DONE.
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
I try to cut down on nested "if" statements whenever possible and make use of short-circuit evaluation, unless the expression in the "if" statement gets to be a little complex/long-winded.
 

presidentender

Golden Member
Jan 23, 2008
1,166
0
76
Originally posted by: txrandom
Originally posted by: presidentender
Originally posted by: txrandom
or just do:

if ($var == 3) {
echo $var;
}

That squawks if $var isn't set.

I'm not getting any errors when I do it. I got display errors set to on.

Alright, I know what I'm doing wrong. When using stuff from inside an array (for me, this is usually $_GET) it'll throw an "undefined offset" error, so I need to use the isset() for that. $var==3 is fine if $var isn't set. I was wrong.

(of course, maybe I still am)
 

NiKeFiDO

Diamond Member
May 21, 2004
3,901
1
76
Originally posted by: txrandom
Originally posted by: presidentender
Originally posted by: txrandom
or just do:

if ($var == 3) {
echo $var;
}

That squawks if $var isn't set.

I'm not getting any errors when I do it. I got display errors set to on.

It will only give a warning, not an error.