search
Homephp教程php手册PHP If Else(elsefi) statement_php basics

Conditional statement

When you write code, you often need to perform different actions for different decisions.

You can use conditional statements in your code to accomplish this task.

if...else statement

Execute a block of code when the condition is true, and execute another block of code when the condition is not true
The elseif statement
is used in conjunction with if...else to execute a code block If... when one of several conditions is true. Else Statement
If you want to execute some code when a certain condition is true and other code when the condition is not true, use the if....else statement.

Syntax

if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

Example

If the current date is Friday, the following code will output "Have a nice weekend!", otherwise it will output "Have a nice day!":

Copy code The code is as follows:



$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>


If you need to execute multiple lines of code when a condition is true or false, you should enclose the lines of code in curly braces:

Copy code The code is as follows:



$d=date("D");
if ($d=="Fri")
{
echo "Hello!
";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>


ElseIf Statement
If you want to execute code when one of multiple conditions is true, use the elseif statement:

Syntax

Copy code The code is as follows:

if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

Example
If the current date is Friday, the following example will output "Have a nice weekend!", if it is Sunday, it will output "Have a nice Sunday!", otherwise it will output "Have a nice day!" ":

Copy code The code is as follows:



$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun" )
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor