PHP If ... Else Simple Tutorial

Source: Internet
Author: User

PHP If ... Else Simple Tutorial

, if ElseIf's reports and other PHP are used to perform different actions, depending on the conditions.


-------------------------------------------------- ------------------------------

Conditional statement
Often when you write code, you want to perform different actions for different decisions.

You can do this by using the code in the conditional statement.

If ... else statement-Use this declaration if you want to execute the code set when one condition is true, the other case if it is not correct
ElseIf's statement-is used if. .. The e LSE statement executes a set of code if one of the conditions is true

-------------------------------------------------- ------------------------------

The If ... else statement
If you want to execute some code, if a condition is true, if the condition of another code is false, if you use .... else statement.

Grammar

if (condition)
  code to was executed if condition is true;
Else
  code to was executed if condition is false;
  
  

For example
The following example will output "wish you a happy weekend!" "If the current day is Friday, otherwise it will produce" Wish you a day! ” :



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

For example
The following example will output "wish you a happy weekend!" "If the current day is Friday, otherwise it will produce" Wish you a day! ” :



     
<?php
$d =date ("D");
if ($d = = "Fri")
  {
  echo "hello!<br/>"; 
  echo "Have a nice weekend!";
  echo "in monday!";
>
</body>
 
       
           

in ElseIf declaration
if you want to execute some code if one of the conditions is true using ElseIf's declaration

Syntax

 if (
Condition) code to was executed if condition is true;
ElseIf (condition) code to was executed if condition is true;

  Else code to was executed if condition is false; 
 
         
             

For example
The following example will output "wish you a happy weekend!" "If the present day is Friday, and" Wish you Sunday! "If the present day is Sunday. Otherwise, the output "Wish you a day!" ":

  
          
 <?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>  
         

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.