Studyon Minte9.com
ZCE 5.3

Study

Operators



	// $a = 1; echo $a++; // output 1
	// $a = TRUE; echo ++$a; // 1
	// echo $str1 += $str2; // 0
	// XOR (true if both operands evaluates to true, but not both)


* Arithmetic Operators

If the operator is placed after its operand, the interpreter will first return the value of the latter (unchanged), 
and then either increment or decrement it by one. Use with care.

<?php $a = 1; echo $a++; // output 1 echo $a; // output 2 $a = 1; echo ++$a; // output 2 echo $a; // output 2
  Incrementing or decrementing booleans has no effect
<?php $a = TRUE; echo ++$a; // 1 echo $a; // 1
  PHP follows Perl's convention when dealing with arithmetic operations on character variables.
<?php $i = 'W'; for ($n=0; $n<6; $n++) { echo ++$i . "n"; } // output X Y Z AA AB AC
  * String Concatenation Operator Unlike many other languages (+ in Java), PHP has a special operation that can be used to concatenate two strings together.
<?php $a = "a"; $b = "b"; echo $a .= $b; // ab
  It is important to remember that this is the only way. Using the addition operator will result in the two strings being first converted to numeric values, and then added together.
<?php $a = "a"; $b = "b"; echo $a += $b; // 0
  * Comparison Operators *** Equivalence Operator (== / !=) Evaluates Evaluates to true if the two operands are equivalent. *** Identity (=== / !==) Evaluates to true only if the operands are of the same data type and have the same value.
<?php $domain = 'xn--google.com'; echo $finded = (stripos($domain, 'xn--') === 0) ? "true" : "false"; // Output: true $domain = 'google.com'; echo $finded = (stripos($domain, 'xn--') == 0) ? "true" : "false"; // Output: true // Wrong (FALSE == 0 returns TRUE) // Identical operator must be used insteed
  It's easy to confuse the assignment operator = for the comparison operator ==
<?php echo $a == 10; echo 10 == $a; // better
  These two operations are completely identical, but, because the left-hand operator of an assignment must be a variable, if you had forgotten one of the equal signs, the parser would have thrown an error, thus alerting you to your mistake.
<?php $a = 9; if ($a == 10) {} if ($a = 10) {} // No alert error if (10 = $a) {} // Parse error: syntax error, unexpected '='
  *** Inequality (< / <= / > / >=) While the process is clear for numbers, things change a bit for other data types.
<?php $left = "ABC"; $right = "ABD"; echo (int) ($left > $right); // output 0 // because the letter D in $right is higher // than the corresponding letter C in $left $left = 'apple'; $right = 'Apple'; echo (int) ($left > $right); // output 1 // because the ASCII value of the character a (97) is // than that of the character A (65)
  * Logical Operators (&& / || / XOR) It's important to understand that all logical operators onlywork with Boolean values Therefore, PHP will first convert any other value to a Boolean and then perform the operation. *** AND operator (&& / and) Evaluates to true if both the left and right operands evaluate to true. *** OR operator (|| / or) Evaluates to true if either the left or right operands evaluate to true. *** XOR operator Evaluates to true if either the left and right operands evaluates to true, but not both * Other operators *** Error suppression operator (@) Causes PHP to ignore almost all error messages that occur while that expression is being evaluated *** Backtick operator (`) Makes it possible to execute a shell command and retrieve its output. Shortcut for shell_exec()) with the same permissions as the webserver.
<?php $a = `ls -l`;
  Don't confuse the backtick operator with regular quotes.