elseif/else if

(PHP 4, PHP 5, PHP 7) Examples: elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would displ

else

(PHP 4, PHP 5, PHP 7) Examples: Often you'd want to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. For example, the following code would display a is greater than b if $a is greater than $b,

if

(PHP 4, PHP 5, PHP 7) Examples: The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C: if (expr) statement As described in the section about expressions, expression is evaluated to its Boolean value. If expressio

Type Operators

Examples: Using instanceof with classes instanceof is used to determine whether a PHP variable is an instantiated object of a certain class: <?php class MyClass { } class NotMyClass { } $a = new MyClass; var_dump($a instanceof MyClass); var_dump($a instanceof NotMyClass); ?> The above example will output: bool(true) bool(false) Using instanceof with inherited classes instanceof can also be used to determine whether a variable i

Array Operators

Examples: The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored. <?php $a = array("a" => "apple", "b" => "banana"); $b = array("a" => "pear", "b" => "strawberry", "c" => "cherry"); $c = $a + $b; // Union of $a and $b echo "Union of \$a and \$b: \n"; var_dump($c); $c = $

String Operators

Examples: There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information. <?php $a = "Hello "; $b = $a . "World!"; // now $b contains "Hello World!" $a = "Hello "; $a .= "World!";     // now 

Logical Operators

Examples: Logical operators illustrated The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See Operator Precedence.) <?php // -------------------- // foo() will never get called as those operators are short-circuit $a = (false && foo()); $b = (true  || foo()); $c = (false and foo()); $d = (true  or  foo()); // -------------------- // "||" has a greater precedence than "or" // The res

Incrementing/Decrementing Operators

Examples: Here's a simple example script: <?php echo "<h3>Postincrement</h3>"; $a = 5; echo "Should be 5: " . $a++ . "<br />\n"; echo "Should be 6: " . $a . "<br />\n"; echo "<h3>Preincrement</h3>"; $a = 5; echo "Should be 6: " . ++$a . "<br />\n"; echo "Should be 6: " . $a . "<br />\n"; echo "<h3>Postdecrement</h3>"; $a = 5; echo "Should be 5: " . $a-- . "<br />\n"; echo "Should be 4: " . $a . 

Execution Operators

Examples: PHP supports one execution operator: backticks (``). Note that these are not single-quotes! PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned (i.e., it won't simply be dumped to output; it can be assigned to a variable). Use of the backtick operator is identical to shell_exec(). <?php $output = `ls -al`; echo "<pre>$output</pre>"; ?>

Error Control Operators

Examples: PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. If you have set a custom error handler function with set_error_handler() then it will still get called, but this custom error handler can (and should) call error_reporting() which will return 0 when the call that triggered the error was preceded by an @. If the track_errors featu