views:

537

answers:

6

I have an interesting question about the way PHP evaluates boolean expressions. When you have, for example,

$expression = $expression1 and $expression2;

or

if ($expression1 and $expression2)

PHP first checks if $expression1 evaluates to true. If this is not the case, then $expression2 is simply skipped to avoid unnecessary calculations. In a script I am writing, I have:

if ($validator->valid("title") and $validator->valid("text"))

I need to have the second statement ($validator->valid("text")) evaluated even if the first one evaluates to false. I would like to ask you whether there is some easy way to force PHP to always evaluate both statements. Thank you!

+15  A: 
$isValidTitle = $validator->valid("title");
$isValidText = $validator->valid("text");
if($isValidTitle && $isValidText)
{
    ...
}

Will that suit?

Kalium
I like this solution because it's explicitly stated that the methods should be executed as code, not as a simple test. There are schools of thought that believe statements with side effects should never go into control statements - control statements are only for testing, never executing.
Adam Davis
+2  A: 

This might not be the best implementation, but you could always do:

$a=$validator->valid("title");
$b=$validator->valid("text");
if($a && $b) {...}
dmondark
+2  A: 

This is known as short circuit evaluation, and to avoid it you need to do this, using a single &:

if($validator->valid("title") & $validator->valid("text")) {

}

Note that this is not using logical operators but actually bitwise operators:

They're operators that act on the binary representations of numbers. They do not take logical values (i.e., "true" or "false") as arguments without first converting them to the numbers 1 and 0 respectively. Nor do they return logical values, but numbers. Sure, you can later treat those numbers as though they were logical values (in which case 0 is cast to "false" and anything else is cast to "true"), but that's a consequence of PHP's type casting rules, and nothing to do with the behavior of the operators.

As such, there is some debate as to whether it is good practice to use this side effect to circumvent short-circuit evaluation. I would personally at least put a comment that the & is intentional, but if you want to be as pure as possible you should evaluate whether they are valid first and then do the if.

Paolo Bergantino
Paul Biggar
I am perfectly aware of the limitations, did you even read my answer?
Paolo Bergantino
@Paolo Bergantino: I did. I wanted to point out that this shouldn't be chosen as the correct answer, clever as it is (well, its too clever really).
Paul Biggar
+3  A: 

try to evaluate each term separately:

$term1 = $validator->valid("title");
$term2 = $validator->valid("text");
if($term1 && $term2) {
//things to do
}
chosta
A: 

Alternatively, if you can modify the class $validator instantiates, you could make the valid method accept a string or an array. If it's an array, it runs the code that already exists on each item and only returns TRUE if all items are "valid".

Matt
A: 

You can define a function like:

function logical_and($x,$y) {return ($x && $y);}

Since PHP uses call-by-value, this works.

Lajla