Henry van Megen Henry van Megen - 3 months ago 15
PHP Question

Why don't I get a PHP notice that the array index does not exist if I put an isset() in the same IF-statement?

Situation: We have a small piece of PHP code which checks if a variable has a certain value, but since the array index and it's value haven't been set at all, this will create an E_NOTICE error:

This triggers a notice



<?php

$arr = array();

if ($arr['name']=='john') {
echo('This should not work');
} else {
echo('In your computer, ignoring your notice');
}


This throws a nice notice:

PHP Notice: Undefined index: name in /home/hvanmegen/test.php on line 5
PHP Stack trace:
PHP 1. {main}() /home/hvanmegen/test.php:0


Now please explain to me, if that value check throws an error, HOW does the code printed below work just fine without throwing that same exception?!?!

This doesn't triggers a notice!



<?php

$arr = array();

if (isset($arr['name']) && $arr['name']=='john') {
echo('This should not work');
} else {
echo('In your computer, ignoring your notice');
}


Output:

In your computer, ignoring your notice


Are AND-checks just aborted/thrown out as soon as the first false gets returned or something?



Does PHP just skip the second check and never actually perform the second check whenever the first false is triggered in a list of comparisons??

Also, is it considered good coding practise to separate the two if statements or am I being a bit too old fashioned with my coding here? One would think that the correct way to check this would be this:

<?php

$arr = array();

if (isset($arr['name'])) {
if ($arr['name']=='john') {
echo('Hi john');
} else {
echo('Hi not-john, where is john?');
}
} else {
echo('I cannot check what I do not have');
}


Output:

I cannot check what I do not have


Is it just my OCD that wants to put the 'isset()' outside the other statement or is my approach totally superfluous and outdated? Or am I clinging to the idea that you should write code in as much 'common tongue' (use language independent syntax, to make switching from one language to the next as easy as possible) as you can?

How do other programming and scripting languages (Java, C++/C#, Javascript, Python) handle this?

Answer

Does PHP just skip the second check and never actually perform the second check whenever the first false is triggered in a list of comparisons??

Yes. It's called short-circuit evaluation.

&& (and) doesn't evaluate the right-hand side if the left-hand side is false, since the result must be false.

|| (or) doesn't evaluate the right-hand side if the left-hand side is true, since the result must be true.

Also, is it considered good coding practice to separate the two if statements or am I being a bit too old fashioned with my coding here?

Although coding style is very personal, it's not necessary to separate them once you understand short-circuit evaluation. I believe most programmers would keep the conditions in one if in this case, particularly since they are closely related and are basically two steps towards checking the same thing.

How do other programming and scripting languages (Java, C++/C#, JavaScript, Python) handle this?

The same as PHP.

Comments