switch

(PHP 4, PHP 5, PHP 7) Examples: switch structure The following two examples are two different ways to write the same thing, one using a series of if and elseif statements, and the other using the switch statement: <?php if ($i == 0) {     echo "i equals 0"; } elseif ($i == 1) {     echo "i equals 1"; } elseif ($i == 2) {     echo "i equals 2"; } switch ($i) {     case 0:         ech

while

(PHP 4, PHP 5, PHP 7) Examples: while loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a while statement is: while (expr) statement The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. The value of

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

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

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

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 

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>"; ?>

Comparison Operators

Examples: If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value. <?php var_dump(0 == "a"); // 0 == 0 -> true var_dump("1" == "01"); // 1 == 1 -> true var_dump("10" ==