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

continue reading.

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!”; } ?>

continue reading.

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

continue reading.

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

continue reading.

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

continue reading.

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

continue reading.

PHP Foreach Loop

Loops through a block of code for each element in an area. Syntax <?php foreach ($array as $value) { code to be executed; } ?> Example The following example will output the values of the given array ($colors): <?php $colors = array(“yellow”, “green”, “blue”, “golden”); foreach ($colors as $value) { echo “$value “; } ?>

continue reading.