PHP basic if else, else if statement usage detailed explanation

伊谢尔伦
Release: 2023-03-11 07:34:01
Original
2554 people have browsed it

Conditional statements

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.

if...else Statement

Execute a block of code when the condition is true, and execute another block when the condition is not true Code
elseif statement
Use with if...else to execute a block of code when one of several conditions is trueIf...Else statement
If you want to execute some code when a certain condition is true , to execute other code when the condition is not true, please use if....else statement.

Syntax

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

Example

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

<html>
<body>
<?php
$d=
date
("D");
if ($d=="Fri")
  
echo
 "Have a nice weekend!"; 
else
  echo "Have a nice day!"; 
?>
</body>
</html>
Copy after login

If you need to execute multiple lines of code when the condition is true or not, these lines of code should be included In curly braces:

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  {
  echo "Hello!<br />"; 
  echo "Have a nice weekend!";
  echo "See you on Monday!";
  }
?>
</body>
</html>
Copy after login

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

Syntax

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;
Copy after login

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

<html>
<body>
<?php
$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!"; 
?>
</body>
</html>
Copy after login


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

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!