6

I was going through operator precedence section of php.net 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 lot of 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 statements in between the code, allowing me to control these print(_r) statements. This comes from my BASH experience where I used to write lot of [[ $condition ]] && { #Do if true } || { #Do if false }. In BASH, I knew they are short circuited and used this fact to write lot of simple one liners.
Now I am observing that lot of this practice(of writing $verbose and print) is slowly creeping into my if statements. I am aware this is NOT a recommended practice and can bite me in the back. However, I do want to master this skill as I enjoy writing such one liners and want to use it as my personal style.

So my question(s) is(are) :

  1. Which operator (&& or and) is short circuited ?
  2. The manual says && takes precedence over and, but can someone exemplify this by mixing the short circuited operator feature/functionality/characteristic with operator precedence. (basically mix and match of precedence and short-circuiting)

Kindly elaborate on both the short-circuiting as well as return value nature of the operators.

PS: 1. I hope associativity of these operators is same and intuitive, but if you know any quirks, please enlighten me.
PS: 2. If you still feel like warning me against the perils of such practice, kindly include examples.

EDIT : After changing my simple $var = mysql_(...) or die() code by replacing or with ||, I discovered how annoying it can be to use the || and && operators instead of and and or. The code simply didn't work ! To my understanding, the former construct assigns a return value of TRUE or FALSE to $var, which in turn make all sequential use of $var to generate warning/error/unexpected behavior. The latter construct assigns result of mysql_(...) to $var first and then evaluates the compound of = and die.
This is a good lesson for me, I better 1. Start using PDO/mysqli and handle errors on my own 2. Think twice before writing something I called above as personal style.
//note to self : don't use experience of one scripting/interpretive language while writing code in another, each one is unique and has its own quirks and pitfalls, and thats just sad *sigh*

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
wadkar
  • 960
  • 2
  • 15
  • 29
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon Aug 08 '11 at 09:59
  • @Gordon : Thank you for the _Reference_. I checked the list and though couple of questions seem to answer part of the questions (like precedence, which I knew already). IMHO the short-circuit nature of the operators along with their precedence isn't discussed in detail. – wadkar Aug 08 '11 at 10:07
  • The author of the comment on the example link mentions operator precedence which indicates that the AND keyword is being evaluated after the equals. It appears odd but it reminds me of some of the `mysql_connect(...) or die()` lines some scripts use. – James P. Aug 08 '11 at 10:08
  • Related: http://stackoverflow.com/questions/3220919/php-short-circuit-lazy-evaluation-where-is-it-in-the-php-net-manual – James P. Aug 08 '11 at 10:12
  • indeed ! I use `mysql_connect(...) or die()` on daily basis. I must 1. shift to PDO, 2. Always use `&&` and `||`. @James thanks – wadkar Aug 08 '11 at 10:26
  • You're welcome. This link might be of use to you. I'm not sure how readable it will be through Google translate but the code is pretty much clear: http://www.alsacreations.com/tuto/lire/676-gestion-erreurs-mysql-php-or-die.html – James P. Aug 08 '11 at 10:28
  • thanks @James I will use PDO for future code, for now I have replaced all `or` with `||` (and `and` with `&&`). – wadkar Aug 08 '11 at 11:39
  • If someone is reading my above comment, see the edit in the Q. Don't start replacing `and` with `&&`s and `or` with `||`s. You will break the whole thing if you are not cautious. – wadkar Aug 08 '11 at 12:43

1 Answers1

7

The code

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

is parsed the same as

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

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

user2864740
  • 60,010
  • 15
  • 145
  • 220
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • @Felix : can you please write/explain/elaborate about short circuiting in PHP? I am specially concerned about the return value that gets generated after the short circuiting. – wadkar Aug 08 '11 at 12:40
  • 1
    @Sudhi: I don't understand what you want me to write. All these operators (`&&`, `||`, `and`,`or`) are short circuiting... the return value is always a boolean value. – Felix Kling Aug 08 '11 at 13:02
  • What if && and || are mixed in the same expression? are the &&'s calculated first, or is the evaluation strictly left-to-right? – David Spector Aug 02 '19 at 13:11