Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP if... Else statement


May 10, 2021 PHP


Table of contents


PHP i f... Else statement

This section describes PHP if... The use of else statements, through which you can selectively execute snippets of code.


Conditional statements are used to perform different actions based on different conditions.


PHP conditional statement

When you write code, you often need to perform different actions for different judgments. You can use conditional statements in your code to accomplish this task.

In PHP, the following conditional statements are provided:

  • If statement - Executes the code when the condition is established
  • if... Else statement - Executes one piece of code when a condition is established and another piece of code when a condition does not
  • if... e lse if.... Else statement - Executes a block of code when one of several conditions is established
  • Switch statement - Executes a block of code when one of several conditions is established

PHP - if statement

The if statement is used to execute code only when the specified condition is established.

Grammar

if ( condition )
{
Code to be executed when the condition is established
;
}

If the current time is less than 20, the following example outputs "Have good day!"

<?php
$t=date("H");
if ($t<"20")
{
echo "Have a good day!";
}
?>

Run an instance . . .


PHP - if... Else statement

If one piece of code is executed when the condition is established and another piece of code is executed when the condition is not, use if.... Else statement.

The meaning of the statement is: code when the condition is true, code when the condition is true, and code when the condition is false, code when the condition is not true, the syntax is as follows:

Grammar

if ( condition )
{
The code that is performed when the condition is established;
}
else
{
Code that is not performed when the condition is not established;
}

If the current time is less than 20, the following example outputs "Have a good day!" or "Have a good night!"

<?php
$t=date("H");
if ($t<"20")
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
?>

Run an instance . . .


PHP - if... e lse if.... Else statement

To execute a block of code when one of several conditions is established, use if.... e lse if... Else statement.

Grammar

if ( condition )
{
The code executed when the IF condition is established;
}
else if ( condition )
{
The code executed when the ELSE IF condition is established;
}
else
{
Code that is not performed when the condition is not established;
}

If the current time is less than 10, the following instance outputs "Have a good morning!", or "Have a good night!" if the current time is not good less than 10 and less than 20.

<?php
$t=date("H");
if ($t<"10")
{
echo "Have a good morning!";
}
else if ($t<"20")
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
?>

Run an instance . . .


PHP - switch statement

In the next section, you'll learn more about switch statements.