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

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 

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 = $

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

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,

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

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

do-while

(PHP 4, PHP 5, PHP 7) Examples: do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it may not necessarily ru

for

(PHP 4, PHP 5, PHP 7) Examples: for loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a for loop is: for (expr1; expr2; expr3) statement The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, expr2 is evaluated. If it evaluate