updated on: 19-Dec-2022
if (expr)
statement
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
The following example would display a is bigger than b if $a is bigger than $b:
<?php
if ($a > $b)
echo "a is bigger than b";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - The if...elseif....else Statement</title>
</head>
<body>
<?php
// i for Minutes with leading zero, s for Seconds with leading zero
echo "Minutes:" . date("i");
echo "<br> Seconds:" . date("s") . "<br>";
//
// if..elseif..else
//
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
</body>
</html>
further reading:
<?php
// This switch statement:
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
// Is equivalent to:
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
?>
<?php
switch ($i) {
case 0:
echo "i equals 0";
case 1:
echo "i equals 1";
case 2:
echo "i equals 2";
}
?>
The statement list for a case can also be empty, which simply passes control into the statement list for the next case.
<?php
switch ($i) {
case 0:
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
?>
A special case is the default case. This case matches anything that wasn’t matched by the other cases. For example:
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
A case value may be given as an expression. However, that expression will be evaluated on its own and then loosely compared with the switch value. That means it cannot be used for complex evaluations of the switch value. For example:
<?php
$target = 1;
$start = 3;
switch ($target) {
case $start - 1:
print "A";
break;
case $start - 2:
print "B";
break;
case $start - 3:
print "C";
break;
case $start - 4:
print "D";
break;
}
// Prints "B"
?>
It’s possible to use a semicolon instead of a colon after a case like:
<?php
switch($equipments)
{
case 'keyboard';
case 'mouse';
case 'motherboard';
case 'speaker';
echo 'Good choice';
break;
default;
echo 'Please make a new selection...';
break;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - Switch</title>
</head>
<body>
<?php
$favcolor = "yellow";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
</body>
</html>
further reading:
for (expr1; expr2; expr3) statement
Consider the following examples. All of them display the numbers 1 through 10:
<?php
// example 1
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
// example 2
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}
// example 3
$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
// example 4
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - For Loop</title>
</head>
<body>
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
further reading:
There are two syntaxes:
foreach (iterable_expression as $value)
statement
foreach (iterable_expression as $key => $value)
statement
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
Warning: Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - foreach</title>
</head>
<body>
<?php
$colors = array("red", "green", "blue", "yellow","pink");
foreach ($colors as $i)
{
echo "<h1 style=". "background-color:$i>" . $i ."</h1><br>";
}
?>
</body>
</html>
Some more examples to demonstrate usage:
<?php
/* foreach example 1: value only */
$a = array(1, 2, 3, 17);
foreach ($a as $v) {
echo "Current value of \$a: $v.\n";
}
/* foreach example 2: value (with its manual access notation printed for illustration) */
$a = array(1, 2, 3, 17);
$i = 0; /* for illustrative purposes only */
foreach ($a as $v) {
echo "\$a[$i] => $v.\n";
$i++;
}
/* foreach example 3: key and value */
$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach ($a as $k => $v) {
echo "\$a[$k] => $v.\n";
}
/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
/* foreach example 5: dynamic arrays */
foreach (array(1, 2, 3, 4, 5) as $v) {
echo "$v\n";
}
?>
Further reading:
while (expr)
statement
while (expr):
statement
…
endwhile;
The following examples are identical, and both print the numbers 1 through 10:
<?php
/* example 1 */
$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
/* example 2 */
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - While Loop</title>
</head>
<body>
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
</body>
</html>
further reading:
do {
code to be executed;
} while (condition is true);
further reading:
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
The above loop would run one time exactly, since after the first iteration, when truth expression is checked, it evaluates to false ($i is not bigger than 0) and the loop execution ends.
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - Do While </title>
</head>
<body>
<?php
/*$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);*/
// example2
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
</body>
</html>
Muhammad Yasir Bhutta