## PHP Operators

PHP language has following types of operators. Arithmetic Operators Comparison Operators Logical/Relational Operators Assignment Operators Conditional/ternary Operators Arithmetic Operators Operator Description Example + Adds two operands A + B will give 30 – Subtracts second operand from the first A – B will give -10 * Multiply both operands A * B will give 200 / Divide numerator by de-numerator B / A will give 2 % Modulus Operator and the remainder of after an integer division B % A will give 0 ++ Increment operator, increases integer value by one A++ will give 11 — Decrement operator, decreases integer value by one A– will give 9 Comparison Operators Operator Description Example == Checks if the value of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != Checks if the value of two operands are equal or not, if values are not equal then the condition becomes true. (A != B) is true. > Checks if the value of the left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. < Checks if the value of the left

## PHP If Else

The if, elseif …else and switch statements are used to take decisions based on the different conditions. In PHP we have the following conditional statements: if() statement – executes some code if one condition is true if()…else statement – executes some code if a condition is true and another code if that condition is false if()…elseif()…else statement – executes different codes for more than two conditions switch statement – selects one of many blocks of code to be executed

## PHP Switch Statement

The switch statement is used to make a decision from the number of choices is called a switch-case-default. The switch statement is the same as a series of IF statements on the same expression. Syntax <?php switch (n) { case condition1: code to be executed if n=condition1; break; case condition2: code to be executed if n=condition2; break; case condition3: code to be executed if n=condition3; break; … default: code to be executed if n is different from all labels; } ?> Example: In the following example, \$color is equal to green, therefore the switch statement executes the third echo statement. <?php \$color = “green”; switch (\$color) { case “yellow”: echo “color is yellow!”; break; case “blue”: echo “color is blue!”; break; case “green”: echo “color is green!”; break; default: echo “color is neither yellow, blue, nor green!”; } ?>

## PHP Loops

4 Types of Loops in PHP PHP for loop can be used to repeat the set of code for the specified number of times. while() — Loops through a block of code as long as the condition specified equal to true. do()…while() — The block of code executed once and then the condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true. for() — PHP for loop can be used through a set of codes for the specified number of times. foreach() — Loops through a block of code for each element in an area

## PHP While Loop

Loops through a block of code as long as the condition specified equal to true. Syntax <?php while (when condition is true) { code to be executed; } //or while(condition): //code to be executed endwhile; ?> Examples The example below displays the numbers from 1 to 6: <?php \$x=1; while(\$x

## PHP Do While Loop

The block of code executed once and then the condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true. It executes the code at least one time always because the condition is checked after executing the code. Syntax <?php do { code to be executed; } while (condition is true); ?> Example This example sets the \$x variable to 7, then it runs the loop, and then the condition is checked: <?php \$x = 7; do { echo “The number is: \$x”; \$x++; } while (\$x <= 5); ?>

## PHP For Loop

PHP for loop can be used through a set of codes for the specified number of times. Syntax <?php for(initialization; condition; increment/decrement){ //code to be executed } ?> Example The example below return the numbers from 0 to 5: <?php for (\$a = 0; \$a