What Are The Conditional Statements In PHP?

What Are The Conditional Statements In PHP?

The PHP Conditional statement is used to perform different conditions based on various actions.

When you write a code in PHP, and you want to perform different conditions, then you can use conditional statements.

In the PHP programming language, we have several conditional statements, which are:

  • If statement.
  • If ….else statement.
  • If …elseif.. else statement.
  • Nested if statement.
  • Switch statement.

1. If –Statement:

In PHP programming, the “if statement” executes the code if the condition is TRUE.

The “if statement” is used to check some conditions.

Let’s suppose some conditions, such as any number is less than 100 or more.

Syntax:

If (condition)

{

//code to be executed if condition is TRUE.

}

Let’s look at an example of a grade system—

 

Output : 50 is less than 100.

if-statement

2. If..else –Statement:

The PHP “if-else statement” is performed for verifying if the condition is TRUE or FALSE.

In PHP, the “If else statement” is used to check any type of condition. If the condition is true, then it will come under the If block and, if the condition is false, it will fall under the Else block.

For example: – We are checking whether a number is Odd or Even by using the “If else statement”.

Syntax:

If (condition){

//code to be executed if condition is TRUE.

}

else{

//code to be executed if condition FALSE.

}

Let’s look at an example of a grade system—

if-else-statement

Output: the 10 is even number.

3. If..elseif..else –Statement:

The PHP programming “if-elseif-else statement” is used to check the multiple statements. In PHP programming language, when we want to execute the code for a different set of conditions, and we have more than two possible conditions then we use the “if…else if…else statement”.

Syntax:

if (condition1){

//the code to be executed if condition1 is TRUE.

}

elseif (condition2){

//the code to be executed if condition2 is TRUE.

}

elseif (condition3){

// the code to be executed if condition3 is TRUE.

}

elseif(condition4){

// the code to be executed if condition4 is TRUE

}

else{

//the code to be executed if all the given conditions are FALSE.

}

Let’s look at an example of a grade system—

if-else-if-else-statement

Output: You get ‘C’ grade.

4. Nested if Statement

In the PHP programming language nested if statement is the if block inside another if block, And the inner if statement implements only when a specific condition in the outer if statement is TRUE.

For example- The eligible age of an Indian citizen to access the right to vote is 18 years. If we want to check whether a person is eligible to vote or not, we can use the Nested If statement.

Syntax:

If(condition){

//the code to be executed If condition is TRUE.

If(condition){

// the code to be executed If condition is TRUE.

}

}

Let’s look at an example of a grade system—

nested-if-statement

Output: You eligible to give vote

5. Switch –Statement:

The PHP programming switch statement is used to check the multiple conditions. It works like a PHP if-else-if statement.

 

The important points that are noticed in Switch case are as follows: –

  1. The Default is not very important and is an optional statement in the switch case. It must always be the last statement.
  2. The switch case statement can only have only one default as multiple defaults lead to a fatal error.
  3. There should be a break statement in each case that can be used for the termination of the sequence of statements.
  4. In switch cases, the use of a break statement is optional. In its absence, after finding the matched case value all the statements will be executed. It is allowed to use numbers characters
  5. strings and functions in the switch expression of PHP.
  6. Though Nesting is not prohibited, it will only lead to more complexity and less readability of switch statements.
  7. The use of a semicolon (;) in place of the colon (:) will not create any error.

Syntax:

switch(condition){

case value1:

//code to be execute here.

break;

case value2:

//code to be execute here.

break;

case value3:

//code to be execute here

Break;

……

default:

code to be execute here if all cases are not match;

}

Let’s look at an example to check vowel or consonant

switch-sttement

Output:  put the character A is vowel.

Also Read,

 

Raju Kayal

Raju, a Technical Analyst at Webskitters Academy, have several years of experience in the web development industry. He believes in sharing his knowledge and understanding and encourage young aspirants in the field, through his writings. His competence and passion encourage him to resolve the queries of the knowledge-seekers and polish their skills.

What Students are Saying

Students Get Placed in

What Our Clients Are Saying

Call Request Form