Does PHP have a short circuit rating?

Given the following code:

if (is_valid($string) && up_to_length($string) && file_exists($file)) { ...... } 

If is_valid($string) returns false , is_valid($string) php interpreter still check later conditions like up_to_length($string) ?
If so, then why does he do extra work when it is not needed?

+65
php if-statement short-circuiting
Apr 17 '11 at 16:25
source share
10 answers

Yes, the PHP interpreter is "lazy", that is, it will make the minimum number of comparisons to evaluate conditions.

If you want to verify this, try the following:

 function saySomething() { echo 'hi!'; return true; } if (false && saySomething()) { echo 'statement evaluated to true'; } 
+92
Apr 17 '11 at 16:28
source share

Yes it is. Here's a little trick that is based on evaluating a short circuit. Sometimes you may have a small if statement that you would prefer to write as ternary, for example:

  if ($confirmed) { $answer = 'Yes'; } else { $answer = 'No'; } 

It can be rewritten as:

  $answer = $confirmed ? 'Yes' : 'No'; 

But what if the yes block also requires the execution of some function?

  if ($confirmed) { do_something(); $answer = 'Yes'; } else { $answer = 'No'; } 

Well, rewriting as ternary is still possible due to short circuit rating:

  $answer = $confirmed && (do_something() || true) ? 'Yes' : 'No'; 

In this case, the expression (do_something () || true) does nothing to change the overall result of the ternary object, but ensures that the ternary condition remains true , ignoring the return value of do_something() .

+8
Mar 09 '17 at 4:27
source share

Some readings to agree with all of these answers: http://en.wikipedia.org/wiki/Short-circuit_evaluation

+4
Apr 17 '11 at 16:30
source share

No, he no longer checks for other conditions if the first condition is not met.

+2
Apr 17 '11 at 16:28
source share

Bitwise operators are & and | . They always evaluate both operands.

Logical operators are AND , OR , && and || .

  • All four operators evaluate only the right side, if they need it.
  • AND and OR have lower priority than && and || . See an example below.

From the PHP manual:

 // The result of the expression (false || true) is assigned to $e // Acts like: ($e = (false || true)) $e = false || true; // The constant false is assigned to $f before the "or" operation occurs // Acts like: (($f = false) or true) $f = false or true; 

In this example, e will be true , and f will be false .

+2
Jan 24 '17 at 11:04 on
source share

Judging from my research, PHP doesn't seem to have the same && operator as it does in JavaScript.

I conducted this test:

 $one = true; $two = 'Cabbage'; $test = $one && $two; echo $test; 

and PHP 7.0.8 returned 1 , not Cabbage .

0
Jul 11 '19 at 8:47
source share

No no. It is always a good place to optimize the order of conditions.

-one
Apr 17 '11 at 16:27
source share

I created my own logic for evaluating short circuits, unfortunately, this does not seem like fast javascripts syntax, but maybe this is a solution that may come in handy:

 $short_circuit_isset = function($var, $default_value = NULL) { return (isset($var)) ? : $default_value; }; $return_title = $short_circuit_isset( $_GET['returntitle'], 'God'); // Should return type 'String' value 'God', if get param is not set 

I can’t remember where I got the following logic from, but if you do the following:

 (isset($var)) ? : $default_value; 

You can skip the need to write the true condition variable again after the question mark, for example:

 (isset($super_long_var_name)) ? $super_long_var_name : $default_value; 

As a very important observation when using the Ternary Operator in this way, you will notice that if a comparison is made, it will simply convey the value of this comparison, since there is more than just a single variable. For example:

 $num = 1; $num2 = 2; var_dump( ($num < $num2) ? : 'oh snap' ); // outputs bool 'true' 
-one
Nov 30 '15 at 17:01
source share

My choice: not trust Short circuit assessment in PHP ...

 function saySomething() { print ('hi!'); return true; } if (1 || saySomething()) { print('statement evaluated to true'); } 

The second part in condition 1 || saySomething () doesn't matter because it always returns true. Unfortunately saySomething () is evaluated and executed.

I may have misunderstood the exact logic of short trailing expressions, but it doesn't look like β€œit will make the minimum number of comparisons” for me.

In addition, this is not only a performance issue, if you perform assignments inside comparisons, or if you do something that has a value other than a simple comparison, you can complete different results.

Anyway ... be careful.

-5
Jan 14 '12 at 10:21
source share

Note: If you want to avoid lazy checking and fulfill each part of the condition, in this case you need to use logical AND, like this:

 if (condition1 & condition2) { echo "both true"; } else { echo "one or both false"; } 

This is useful when you need, for example, to call two functions, even if the first returns false.

-5
Apr 29 '15 at 0:32
source share



All Articles