Evaluating a Short Circuit Using the And Operator in PHP

I am trying to improve my ninja h4x coding skills and now I am looking at different structures and I have found an example of code that is quite difficult for Google.

I am considering the FUEL framework used in the project.

The pattern that I do not understand

$data and $this->template->set_global($data); 

What keyword is executed in this line of code? It is used in many places within the framework, and this is the first thing I found that uses it.

+6
source share
5 answers

This is a type of short circuit assessment. "From and/&& it follows that both sides of the comparison must be evaluated before TRUE .

The element to the left of and/&& is evaluated as TRUE/FALSE , and if TRUE , the element to the right is executed and evaluated. If the left element is FALSE , execution stops and the right side is not evaluated.

 $data = FALSE; // $this->template->set_global($data) doesn't get evaluated! $data and $this->template->set_global($data); $data = TRUE; // $this->template->set_global($data) gets evaluated $data and $this->template->set_global($data); 

Please note that they should not be actual boolean TRUE/FALSE , but they can also be true / false values ​​according to the rules for evaluating PHP. For more information about evaluation rules, see PHP boolean docs .

+12
source

When you use logical operators, operands (the value on the left and the value on the right) are evaluated as logical, so basically this code will do it shorter:

 $o1 = (Bool)$data; // cast to bool if($o1) $o2 = (Bool)$this->template->set_global($data); // cast to bool 

Edit:

Additional Information:

 $a = 33; isset($a) && print($a) || print("not set"); echo "<br>"; isset($a) AND print($a) OR print("not set"); echo "<br>"; 

Try to comment / expand $a = 33; . This is the difference between && and AND , and between || and OR ( print returns true, which is passed to "1" when converting to a string).

+4
source

This is a valid statement and works as follows:

If $ data is valid (not ', 0 or NULL), run $ this-> template-> set_global ($ data)

This is a quick way to say:

 if ($data) { $this->template->set_global($data); } 

Btw you can also use && instead of and

+2
source

PHP supports both && and and for the logical AND operation, and they usually work the same, except and has a slightly lower operator priority than && : http://php.net/manual/en/language.operators.precedence.php

0
source

This is a logical operator , which means that it takes two operands and returns a logical value - true or false . If both operands are evaluated to true (whatever, but also an empty string, zero or zero in PHP), it will return true , otherwise the result will be false .

Here are the official PHP docs for the and operator: http://www.php.net/manual/en/language.operators.logical.php

 <?php $a = true and false; # FALSE $b = true and 5; # TRUE $c = '' and 0; # FALSE $d = null and true; # FALSE ?> 
-2
source

Source: https://habr.com/ru/post/898692/


All Articles