www.xbdev.net
xbdev - software development
Sunday July 21, 2024
Home | Contact | Support | PHP... a powerful, flexible, fully supported, battle tested server side language ..
     
 

PHP...

a powerful, flexible, fully supported, battle tested server side language ..

 


PHP > Conditional Logic



Conditional logic in PHP, including if statements, switch statements, and logical operators like if-else, and, and or, allows you to control the flow of your code based on specific conditions. By mastering these concepts, you can create more dynamic and flexible PHP applications.


If Statements


If statements allow you to execute certain blocks of code conditionally based on whether a specified condition evaluates to true or false.

$age 25;

if (
$age 18) {
    echo 
"You are a minor.";
} elseif (
$age >= 18 && $age 65) {
    echo 
"You are an adult.";
} else {
    echo 
"You are a senior citizen.";
}


Switch Statements


Switch statements provide an alternative way to evaluate multiple conditions against a single value.

$day "Monday";

switch (
$day) {
    case 
"Monday":
        echo 
"It's Monday!";
        break;
    case 
"Tuesday":
        echo 
"It's Tuesday!";
        break;
    
// More cases...
    
default:
        echo 
"It's not a weekday.";
}


Logical Operators


1. If-Else:


If-else statements allow you to execute different blocks of code based on whether a condition is true or false.

$hour date("H");

if (
$hour 12) {
    echo 
"Good morning!";
} else {
    echo 
"Good afternoon!";
}


2. And (&&) Operator


The and (&&) operator checks if both conditions on its left and right sides are true.

$age 25;
$gender "male";

if (
$age >= 18 && $gender == "male") {
    echo 
"You are an adult male.";
}


3. Or (||) Operator


The or (||) operator checks if either condition on its left or right side is true.

$day "Saturday";

if (
$day == "Saturday" || $day == "Sunday") {
    echo 
"It's the weekend!";
}













 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.