PHP introductory tutorial: If...Else conditional judgment statement_PHP tutorial

WBOY
Release: 2016-07-21 14:53:24
Original
1130 people have browsed it

if, elseif and else statements are used to perform different actions based on different conditions.

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
elseif statement
Used with if…else to execute a block of code when one of several conditions is true
If…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 an if….else statement.

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

Example
The following code will print "Have a nice weekend!" if the current date is Friday, otherwise it will print "Have a nice day!":

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 those lines in curly braces:

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 code to execute when one of multiple conditions is true, use the elseif statement:

Grammar
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!":

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!";
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371374.htmlTechArticleif, elseif and else statements are used to perform different actions based on different conditions. Conditional Statements When you write code, you often need to perform different actions for different decisions. You can...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template