Difference between "&&" and "and": operator priority and short circuit

I went through the priority section of the php.net operator and came across this example which says

$a = 1; $b = null; $c = isset($a) && isset($b); $d = ( isset($a) and isset($b) ); $e = isset($a) and isset($b); var_dump($a, $b, $c, $d, $e); //Result: int(1) NULL bool(false) bool(false) <== I get this bool(true) <== I do not get this 

I use quite a few debugging and verbose print (_r) statements in my code to keep track of where I am in the code. So I use $debug and print_r($dataArray) or $verbose and print "Updating dataArray\n" as separate instructions between code, allowing me to control these print (_r) operations. This comes from my BASH experience, where I used to write a lot of [[ $condition ]] && { #Do if true } || { #Do if false } [[ $condition ]] && { #Do if true } || { #Do if false } . In BASH, I knew that they were short-circuited and used this fact to write many simple liners.
Now I am observing that a lot of this practice (writing $verbose and print ) slowly penetrates my if . I know this is NOT a recommended practice and may bite me in the back. However, I want to master this skill, because I like to write such liners and use it as my personal style.

So my question is (is):

  • Which operator ( && or and ) is shorted?
  • The manual states that && takes precedence over and , but can anyone illustrate this by mixing the operator short-circuit function / characteristic / characteristic with the operator priority. (mainly combination and relevance of priority and short circuit)

Please describe in detail both the short-circuited and the return nature of the operators.

PS: 1. I hope the associativity of these operators will be the same and intuitive, but if you know any quirks, please enlighten me. PS: 2. If you still feel that you are warning me about the dangers of this practice, please provide examples.

EDIT: changing my simple code $var = mysql_(...) or die() , replacing or with || , I found that using the || operators could be annoying and && instead of and and or , the code just didn't work ! In my opinion, the first construct assigns a return value of TRUE or FALSE - $var , which in turn does all the consistent use of $var to generate a warning / error / unexpected behavior. The last construct first assigns the result from mysql_(...) to $var , and then evaluates the connection = and die .
This is a good lesson for me, I’m better 1. Start using PDO / mysqli and handle the errors yourself 2. Think twice before writing what I mentioned above as a personal style.
// pay attention to yourself: do not use the experience of one scripting language / interpretation when writing code in another, each of them is unique and has its own quirks and traps, and it's just sad * sigh *

+6
source share
1 answer

The code

 $e = isset($a) and isset($b); 

analyzed the same way

 ($e = isset($a)) and isset($b); 

Therefore, $e , as defined by isset($a) and assignment, true - is independent of isset($b) .

+7
source

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


All Articles