Home » PHP Tutorial » PHP if else example | PHP If, Else & Elseif Conditional Statement

PHP if else example | PHP If, Else & Elseif Conditional Statement

php-if-else-example/

We are sharing PHP if else Statement example to get easily understand

PHP if else Statement

In this Chapter we will cover up the basic example of PHP if else statement for beginners to get easily understand the PHP Conditional Statement. Let’s Start to see examples, PHP if else Statement:

Example 1 :

PHP If Statement Example

If statement always returns a true value after execute the code. Here We fully cover up this statement below.

<?php

$var = 40;

If ($var==40)

Print “The value is 40.”;

?>

It means that here the $var variable value is 40.After that if statement checks that the value is it 40 ? Yes. Then it returns true value – “The value is 40.”

Anther PHP If Example

<?php

$str = “String Example”;
If ($str )
Echo “return true”;

?>

Here the $str hold the string value, If statement is checking either the value is a string or else. The Condition is true, then return true value.

PHP Conditional Statements, Example :

  • If – This statement returns only true value.
  • Else – This statement returns false.
  • If-Elseif-Else – This Statement executes different code for more than two  or above conditions.

PHP If Else Statement :

Let’s start discussing about if and else statement syntax. Here We will show up a basic if..else code in PHP Script.

<?php

$day = “Monday”;

If ($day==”Monday”)

Echo “The Day is”.” “.$day;

Else

Echo “The Day is not Matched.”;

?>

Here We have shown if..else syntax that easily understandable. We see that The value assigned in the variable – $day.It means that $day is a String Variable and hold the string value.Now If Condition is checking the day is it “Monday” ? And it shows the statement is true.So it returns true value – The Day is Monday.After that The Echo Function print the true Statement.

PHP Echo Syntax – “The Day is”.” “.$day;

Concatenation Operator Or DOT Operator :

Here if you see one more thing we added.That is we concatinate the String ( The Day is ) and Variable ( $day ) with the help of Concatenation or DOT Operator.

PHP If Elseif and Else Statement :

Here We will see that how to use PHP If Elseif and Else Statement for checking the Code. Let’s Check Our Example

<?php

$day = “Mnday”;
$day1 = “Sunday”;

if($day==”Monday”)
{
echo “The day is Monday”;

}
elseif($day1==”Sunday”)
{
echo “The Day is Sunday”;
}
else
{

// When Two Conditions are  not true

echo “This is not valid String”;
}

?>

PHP if else html :

Here We will use the PHP if else syntax in a HTML Pages. Lets See Our Example

<html>

<head>

<title>PHP if else Example</title>

</head>

<body>

<?php

$str = “This is a String Variable”;
If ($str )
Echo “return true”;

Else

Echo “Invalid String Variable…”;

?>

</body>

</html>

Here, We can easily embed the PHP Code in the HTML Body Section.

 

PHP if statement multiple conditions :

Now We use if statement with multiple condition using PHP Syntax. Lets See Our Example

<?php

$var = 50;

$var =60;

If ($var)

{

Echo “The value is 50”;

}

elseif($var1 ==60)

{

echo “The value is 60 ”

}

else

{

echo “This is invalid Integer Variable..”;

}

?>

In this way we can check multiple statement using if..elseif..else statement.

PHP if else shorthand

Another way We will use ternary operator with the help of If Else Statement.Lets See Example.

<?php

$var = 50;

$result = ( 50 == $var) ? “yes” : “no”;
echo $result;

?>

Watch Our Live Streaming, PHP, Video Tutorial

, , , , , , ,

Leave a Reply

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

*
*