How many types of statements are there in php?

Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing [an empty statement]. Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well. The various statement types are described in this chapter.

See Also

The following are also considered language constructs even though they are referenced under functions in the manual.

  • list[]
  • array[]
  • echo
  • eval[]
  • print

There are no user contributed notes for this page.

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:

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:

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!":

Try it Yourself »

PHP - The switch Statement

The switch statement will be explained in the next chapter.

PHP Exercises



When the program has conditions then statements are used.
the condition means when we want to do something when something will happen, for example, there is a situation where we want to add number when number if even.'
to apply these kinds of situations in a program conditional statements are used.

There are the following types of statements.

  1.  if statement
  2. if-else statement
  3. if-else if statement
  4. nested if statement
  5. switch statement
  6. conditional operator statement

if statement

when the program has only one condition and related statement then if statement is used.

if-else statement

when the program, has only one condition and related two statement then the if-else statement is used.


if-else-if statement

when the program has multiple conditions and related statements if-else-if statement is used.


Nested if statement

when the program has a condition within other condition, there we use nested if statement.

Switch Statement

It is an alternative statement of if-else if statement there is little difference is that relational, logical operators can not be used here.


Conditional operators

It is an alternative to the if-else statement, and it is an inline conditional statement.

Author/Written by: Mr Hemant Singh [Founder]

How many conditional statements are there in PHP?

In PHP, there are 4 different types of Conditional Statements.

What does statement mean in PHP?

A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing [an empty statement]. Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces.

What are compound statements in PHP?

A compound statement allows a group of zero or more statements to be treated syntactically as a single statement. A compound statement is often referred to as a block.

Why conditional statements are used in PHP?

PHP allows you to choose what action to take based on the result of a condition. This condition can be anything you choose, and you can combine conditions to make actions that are more complicated.

Chủ Đề