Control statements in php w3schools


Conditional statements are used to perform different actions based on different conditions.


PHP Conditional Statements

Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.

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 - The if Statement

The if statement executes some code if one condition is true.

Syntax

if (condition) {
  code to be executed if condition is true;
}

Example

Output "Have a good day!" if the current time (HOUR) is less than 20:

<?php
$t = date("H");

if ($t < "20") {
  echo "Have a good day!";
}
?>

Try it Yourself »



PHP - The if...else Statement

The if...else statement executes some code if a condition is true and another code if that condition is false.

Syntax

if (condition) {
  code to be executed if condition is true;
} else {
  code to be executed if condition is false;
}

Example

Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:

<?php
$t = date("H");

if ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

Try it Yourself »


PHP - The if...elseif...else Statement

The if...elseif...else statement executes different codes for more than two conditions.

Syntax

if (condition) {
  code to be executed if this condition is true;
} elseif (condition) {
  code to be executed if first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}

Example

Output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":

<?php
$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!";
}
?>

Try it Yourself »


PHP - The switch Statement

The switch statement will be explained in the next chapter.


PHP Exercises




In the following chapters you will learn how to repeat code by using loops in PHP.


PHP Loops

Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.

Loops are used to execute the same block of code again and again, as long as a certain condition is true.

In PHP, we have the following loop types:

  • while - loops through a block of code as long as the specified condition is true
  • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
  • for - loops through a block of code a specified number of times
  • foreach - loops through a block of code for each element in an array

The following chapters will explain and give examples of each loop type.



All the conditional statements and loop statements are collectively termed as Control Statements.

Conditional Statements:

Conditional statements are used in a code to perform an action only if a particular condition is satisfied.

PHP provides four types of conditional statements. These are:

if:

If statement is used to perform an action if a single condition is true.

..else:

If else statement is used to perform an action if a single condition is true and to perform another action if that condition is false.

..elseif….else:

If elseif else statement is used to perform different actions for different conditions.

switch:

Switch statement is used to select one of the action to perform from multiple actions, relative to the true condition.

Loop Statements:

Loop Statements are used to execute a block of code continuously as long as the condition of the loop is true, and stops only when the condition fails.

PHP provides three types of loop statements. These are:

For loop:

For loop is used to execute a group of action only for a specified number of times.

While Loop:

While loop is used to execute a group of action as long as the specified condition is true.

Do While Loop:

Do While loop is used to execute a block of code at least once and then to repeat the actions as long as the specified condition is true.

Control statements in php w3schools

What are the control statements in PHP?

Introduction. Control statements are conditional statements that execute a block of statements if the condition is correct. The statement inside the conditional block will not execute until the condition is satisfied.

How many types of control statements are there in PHP?

There are two types of Control Structures in PHP: Conditional Statements and Control Loops.

What are the 4 conditional statements in PHP?

PHP Conditional Statements.
The if statement..
The if...else statement..
The if... elseif....else statement..
The switch... case statement..

How do you use Elseif?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to specify many alternative blocks of code to be executed.