Tutorial by Examples

There are only two string operators: Concatenation of two strings (dot): $a = "a"; $b = "b"; $c = $a . $b; // $c => "ab" Concatenating assignment (dot=): $a = "a"; $a .= "b"; // $a => "ab"
$a = "some string"; results in $a having the value some string. The result of an assignment expression is the value being assigned. Note that a single equal sign = is NOT for comparison! $a = 3; $b = ($a = 5); does the following: Line 1 assigns 3 to $a. Line 2 assigns 5 to $a....
The combined assignment operators are a shortcut for an operation on some variable and subsequently assigning this new value to that variable. Arithmetic: $a = 1; // basic assignment $a += 2; // read as '$a = $a + 2'; $a now is (1 + 2) => 3 $a -= 1; // $a now is (3 - 1) => 2 $a *= 2; //...
The order in which operators are evaluated is determined by the operator precedence (see also the Remarks section). In $a = 2 * 3 + 4; $a gets a value of 10 because 2 * 3 is evaluated first (multiplication has a higher precedence than addition) yielding a sub-result of 6 + 4, which equals to 10...
Left association If the preceedence of two operators is equal, the associativity determines the grouping (see also the Remarks section): $a = 5 * 3 % 2; // $a now is (5 * 3) % 2 => (15 % 2) => 1 * and % have equal precedence and left associativity. Because the multiplication occurs first ...
Equality For basic equality testing, the equal operator == is used. For more comprehensive checks, use the identical operator ===. The identical operator works the same as the equal operator, requiring its operands have the same value, but also requires them to have the same data type. For exampl...
PHP 7 introduces a new kind of operator, which can be used to compare expressions. This operator will return -1, 0 or 1 if the first expression is less than, equal to, or greater than the second expression. // Integers print (1 <=> 1); // 0 print (1 <=> 2); // -1 print (2 <=> 1...
Null coalescing is a new operator introduced in PHP 7. This operator returns its first operand if it is set and not NULL. Otherwise it will return its second operand. The following example: $name = $_POST['name'] ?? 'nobody'; is equivalent to both: if (isset($_POST['name'])) { $name = $_P...
For checking whether some object is of a certain class, the (binary) instanceof operator can be used since PHP version 5. The first (left) parameter is the object to test. If this variable is not an object, instanceof always returns false. If a constant expression is used, an error is thrown. The ...
The ternary operator can be thought of as an inline if statement. It consists of three parts. The operator, and two outcomes. The syntax is as follows: $value = <operator> ? <true value> : <false value> If the operator is evaluated as true, the value in the first block will be ...
Variables can be incremented or decremented by 1 with ++ or --, respectively. They can either precede or succeed variables and slightly vary semantically, as shown below. $i = 1; echo $i; // Prints 1 // Pre-increment operator increments $i by one, then returns $i echo ++$i; // Prints 2 // P...
The PHP execution operator consists of backticks (``) and is used to run shell commands. The output of the command will be returned, and may, therefore, be stored in a variable. // List files $output = `ls`; echo "<pre>$output</pre>"; Note that the execute operator and sh...
In PHP, there are two versions of logical AND and OR operators. OperatorTrue if$a and $bBoth $a and $b are true$a && $bBoth $a and $b are true$a or $bEither $a or $b is true$a || $bEither $a or $b is true Note that the && and || opererators have higher precedence than and and or. S...
Prefix bitwise operators Bitwise operators are like logical operators but executed per bit rather than per boolean value. // bitwise NOT ~: sets all unset bits and unsets all set bits printf("%'06b", ~0b110110); // 001001 Bitmask-bitmask operators Bitwise AND &: a bit is set onl...
Members of objects or classes can be accessed using the object operator (->) and the class operator (::). class MyClass { public $a = 1; public static $b = 2; const C = 3; public function d() { return 4; } public static function e() { return 5; } } $object = new MyCl...

Page 1 of 1