If..Else..ElseIf, Switch Statement – PHP Basics

If…Else…ElseIf, Switch Statement – ​​PHP Basics

When you want to implement different conditions in your program, you use the If…Else…ElseIf statement. If one condition is true, the other condition is also true – If statement If one condition is true and the other condition is false – If…else statement If more than two conditions are required – If…else…elseIf statement selects multiple blocks of code One to be executed – Switch statement if…else…elseif statement:
<!DOCTYPE html>
 <html>
 <body>

<?php
 $t = date("H");
 echo " <p> Hey There,\n </p> ";
 if ($t < "10") {
    
    
 echo "Have a good morning!";
 } elseif ($t < "20") {
    
    
 echo "Have a good day!";
 } else {
    
    
 echo "Have a good night!";
 }
 ?>

</body>
 </html>
Output:

Insert image description here

Switch statement:
<!DOCTYPE html>
 <html>
 <body>

<?php
 $favfood = " ";

switch ($favfood) {
    
    
 case "Spaghetti":
 echo "Your favorite food is Spaghetti!";
 break;
 case "Pasta":
 echo "Your favorite food is Pasta!";
 break;
 case "Sandwich":
 echo "Your favorite food is Sandwich!";
 break;
 default:
 echo "Your favorite food is neither Spaghetti, Pasta, nor Sandwich!";
 }
 ?>

</body>
 </html>
Output:

Insert image description here

When $favfood =” “;

Insert image description here

当 $favfood = “Spaghetti”;

Guess you like

Origin blog.csdn.net/qq_37270421/article/details/133255530