Core PHP PHP

PHP 5 if…else…elseif Statements

PHP 5 if…else…elseif Statements

PHP Conditional Statements

Mostly in writing code, we want to perform different different actions for different conditions. We can use conditional statements.

In PHP following conditional statements are available:

  1. if statement – executes some code if condition is true
  1. if…else statement – if condition true execute if otherwise execute else if false.
  2. if…elseif….else statement – if condition is true execute code otherwise check next condition if all conditions are false then auto execute else.

PHP if Statement

PHP if statement executes code if one condition is true.

Syntax

if (condition) {
//write code here for execute if condition is true
;
}

Example

<?php
$name =”Suresh”;

if ($name == “Suresh”) {
echo “Hi Suresh have a nice day!”;
}
?>

 

PHP – The if…else Statement

The if….else statement executes code if a condition is true execute if part otherwise execute else part when condition is false.

Syntax

if (condition) {
//write code here for execute if condition is true;
} else {
//write code here for execute if condition is false;
}

Example

<?php
$name = “Suresh”;

if ($name == “Suresh”) {
echo “Hi Suresh have a nice day!”;
} else {
echo “Hi every body have a nice day!”;
}
?>

 

PHP – The if…elseif….else Statement

The if….elseif…else statement executes if , if true otherwise check elseif if true then execute otherwise execute else part code.

Syntax

if (condition) {
//write code here for execute if condition is true;
} elseif (condition) {
//write code here for execute if condition is true;
} else {
//write code here for execute if condition is false;
}

Example

<?php
$name =”Suresh”;

if ($name == “Suresh”) {
echo “Hi Suresh have a nice day!”;
} else if ($name == “Mukesh”) {
echo “Hi Mukesh have a nice day!”;
} else {
echo “Hi everybody have a nice day!”;
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *