Skip to content

Instantly share code, notes, and snippets.

@dennisdegryse
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save dennisdegryse/286adf5aac49f1d6b066 to your computer and use it in GitHub Desktop.

Select an option

Save dennisdegryse/286adf5aac49f1d6b066 to your computer and use it in GitHub Desktop.
Operator precedence
<?php
$x = 10;
echo $x > 5 ? "1" : false ? "2" : "3";
// ================================ => = echo construct expression
// + + => o1 = ternary operator at root
// -------------------- => o1.x1 = first operand of o1
// + + => o1.x1.o1 = ternary operator at root of o1.x1
// ------ => o1.x1.o1.x1 = first operand of o1.x1.o1
// + => o1.x1.o1.x1.o1 = binary operator at root of o1.x1.o1.x1
// == => o1.x1.o1.x1.o1.x1 = first operand of o1.x1.o1.x1.o1 = 10
// = => o1.x1.o1.x1.o1.x2 = second operand of o1.x1.o1.x1.o1 = 5
// ====== => o1.x1.o1.x1 = first operand of o1.x1.o1 = TRUE
// === => o1.x1.o1.x2 = second operand of o1.x1.o1 = "1"
// ***** => o1.x1.o1.x3 = third operand of o1.x1.o1 is omitted
// ==================== => o1.x1 = evaluate first operand of o1 = TRUE
// === => o1.x2 = evaluate second operand of o1 = "2"
// *** => o1.x3 = third operand of o1 is omitted
// ================================ => = evaluate echo construct expression = "2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment