Home  >  Article  >  Backend Development  >  if else Statement in PHP

if else Statement in PHP

王林
王林Original
2024-08-29 12:40:21541browse

The following article provides an outline on if else Statement in PHP. If else are the basic conditional statements that are normally used to perform different types of actions based on some of the different conditions. When we write any program/code, one has to take decisions(conditions) to perform certain types of tasks. So we need conditional statements(if, else, etc..,) to do our tasks using the code. “If” condition/statement will make the code run only if the condition is true whereas “else” is the condition which will run only if the “if” condition becomes false.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

if(condition statement){
Programming code statements to run only if the IF condition is TRUE
}
if(condition statement){
Another code to be executed with an extra IF condition. (Multiple iF statements can be ignored to if we don’t want)
}
else{
Code Here will run only if the IF condition becomes FALSE
}

Flow Diagram

if else Statement in PHP

How if else Statement works in PHP?

  • if: If function works when the conditions inside of the function are true else compiler will go to else condition if extra if the condition is not there at first.
  • else: Else function works when the if, Extra if conditions fail in the program/code in PHP/any other Programming Language mostly.

Examples of if else Statement in PHP

Given below are the examples of if else Statement in PHP:

Example #1

This is the basic PHP program to know which number is greater which are stored in the number_one, number_two variables using one if and else conditions by comparing the two variable’s values.

Code:

<?php
$number_one = 17;
$number_two = 51;
if ($number_one > $number_two){
echo "$number_one is greater than $number_two";
}else{
echo "$number_two is greater than $number_one";
}
?>

Output:

if else Statement in PHP

Example #2

This is the basic PHP code which is listed below to know whether Today/Present-day is Saturday/Sunday. Date(“d”) will have the value of Today/present day. It will be stored in s variable. By using the conditions of the “s” variable’s value the code will provide the output. If Today is Sat, code will print as “This is Saturday..Have a nice weekend Bro!!” like that and If Today is Sun, the code will print as “Have a Happy Weekend! Now We are in Sunday!! <3” or else it will print “This is not Weekend. Go to the work Now..”

Code:

<?php
$s = date("D");
if($s == "Sat"){
echo "This is Saturday..Have a nice weekend Bro!!";
}
if($s == "Sun"){
echo "Have a Happy Weekend! Now We are in Sunday!! <3 ";
}
else{
echo "This is not Weekend. Go to the work Now..";
}
?>
</p>
<p><strong>Output:</strong></p>


<p><img  src="https://img.php.cn/upload/article/000/000/000/172490642644758.jpg" alt="if else Statement in PHP" ></p>
<h4>Example<strong> #3</strong>
</h4>
<p>This is a PHP program to print “The Person is now Child” If age is either <18 using IF condition statement or “The Person is now Adult” will be printed.</p>
<p><strong>Code:</strong></p>
<pre class="brush:php;toolbar:false"><?php
$age1 = 22;
if($age1 < 18){
echo 'The Person is now Child';
} else{
echo 'The Person is now Adult';
}
?>
</p>
<p><strong>Output:</strong></p>
<p><img  src="https://img.php.cn/upload/article/000/000/000/172490642776045.jpg" alt="if else Statement in PHP" ></p>
<h4>Example<strong> #4</strong>
</h4>
<p>This is the PHP program to print addition, subtraction( by substracting from the big digit to the small digit by comparing number_1, number_2 variable’s value), multiplication, division(dividing the big number with the small number – This is got by comparing number_2, number_1 variable’s value) of the specified two numbers and also checking whether the number_1, number_2 variable’s value is a prime number or not and then also printing Square values of the number_1, number_2 variable’s values. You can change those numbers however you want or else you can have inputs from the users/customers using the input validation form using the HTML if you want.</p>
<p><strong>Code:</strong></p>


<pre class="brush:php;toolbar:false"><?php
$number_1 = 22;
$number_2 = 46;
echo "First Number(number_1) = $number_1";
echo "<br/>";
echo "Second Number(number_2) = $number_2";
echo "<br/>";
echo "Addition of numbers: ";
echo $number_1 + $number_2;
echo "<br/>";
echo "Substraction of numbers: ";
if($number_1>$number_2){
echo $number_1 - $number_2 ;}
else{
echo $number_2 - $number_1;
}
echo "<br/>";
echo "Multiplication of Numbers : ";
echo $number_1*$number_2;
echo "<br/>";
echo "Division of numbers:: ";
if($number_1>$number_2){
echo $number_1/$number_2 ;}
else{
echo $number_2/$number_1;
}
echo "<br/>";
if($number_1%2==0){
if($number_1%3==0){
if($number_1%5==0){
if($number_1%7==0){
echo "1st Number : $number_1 is not prime number";
}
echo "1st Number : $number_1 is not prime number";
}
echo "1st Number : $number_1 is not prime number";
}
echo "1st Number : $number_1 is not prime number";
}
else{
echo "1st Number : $number_1 is a prime number";
}
echo "<br/>";
if($number_2%2==0){
if($number_2%3==0){
if($number_2%5==0){
if($number_2%7==0){
echo "2nd Number : $number_2 is not prime number";
}
echo "2nd Number : $number_2 is not prime number";
}
echo "2nd Number : $number_2 is not prime number";
}
echo "2nd Number : $number_2 is not prime number";
}
else{
echo "2nd Number : $number_2 is a prime number";
}
echo "<br/>";
echo "Square of number_1($number_1) = ";
echo pow($number_1,2);
echo "<br/>";
echo "Square of number_2($number_2) = ";
echo pow($number_2,2);
echo "<br/>";
?>

Output:

if else Statement in PHP

Example #5

This is the PHP Programming code to check whether on which mode a person is traveling using 3 Condition statements with different speed values to the speed1 variable. If the variable has numerical value which is less than 60 then “You are now in a safe driving mode” will be printed or if the speed1 variable is between 60 and 100 then “You are in the mode of burning extra fuel for your vehicle” will be printed or else “You are in the dangerous mode: Accidents may occur please be careful” will be printed. This is the sample example program of PHP to illustrate speed mode using speed1 variables value. One can automate this simple program by connecting the GPS values and check the variation in the mode in PHP/any other programming language too.

Code:

<?php
$speed1 = 110;
if($speed1 < 60)
{
echo "You are now in a safe driving mode";
}
if($speed1 > 60 && $speed1 < 100)
{
echo "You are in the mode of burning extra fuel for your vehicle";
}
else
{
echo "You are in the dangerous mode : Accidents may occur please be careful";
}
?>

Output:

if else Statement in PHP

The above is the detailed content of if else Statement in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP if StatementNext article:PHP if Statement