PHP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP If Else

PHP If Else

shape Description

If Statements are conditional statements, if statement is used to perform the action depending on the condition.

shape Syntax

if (condition) { code to be executed if condition is true; }
3 - Level If Statements exists in PHP:

If -Statement

shape Description

PHP If will execute the code inside the if statement only, when the if condition is true.

shape Example

[php] <!DOCTYPE html> <html> <body> <?php $x = 30; if($x<=30) { echo "SP Lessons"; } ?> </body> </html> [/php] Output:

If...Else Statement

shape Description

PHP If Else will executes the code inside the if statement only if the condition is true, otherwise it will allows to executes else block for the if condition.

shape Example

[php] <!DOCTYPE html> <html> <body> <?php $x = 30; if($x>30) { echo "SPLessons"; } else{ echo "Welcome to SPLessons!!"; } ?> </body> </html> [/php] Output:

If...Elseif...Else Statement

shape Description

First the controller checks the if Statement condition.If the condition true, if statement block executes, otherwise the controller goes to elseif statement.If the condition is true here, the elseif Statement block will execute, otherwise again it will go to the next level else block and the process continues.

shape Example

[php] <!DOCTYPE html> <html> <body> <?php $x = 30; if ($x > 100) { echo "SPLessons"; } elseif($x>=50) { echo "Welcome to SPLessons"; } else { echo "Welcome to SPLessons world!!"; } ?> </body> </html> [/php] Output:

Summary

shape Key Points

  • If statement executes when the if condition is true.
  • PHP If else executes only if the condition of if is true, otherwise it will allows to executes else block for the if condition.