I'm trying to get better in PHP foundations and I have run this simple test on php 7.0.4:
<?php
function am_I_executed(){
echo "yes I am executed\n";
return true;
}
echo "test1: \n";
if (false && am_I_executed()) {
echo "this should never be displayed \n";
}
echo "test2: \n";
if (am_I_executed() && false) {
echo "this should never be displayed \n";
}
The result is:
test1
test2
yes I am executed
Before trying this I thought that the compiler would just skip this if statement because it should always give false, doesn't matter what's the return of am_I_executed()
and that there would be no difference between test1 and test2
I was obviously wrong.
I believe that in real life experience it might happen a situation like this one:
if(algorithm1() && algorithm2()){}
but in some occasion it might be hard to understand which is the lighter algorithm and then decide in which order they should be listed in the if statement.
How do you tackle this problem?