PHP If…Else…Elseif Statements

PHP If…Else…Elseif Statements

In PHP, you can use if, else, and elseif (or else if) statements to control the flow of your program based on certain conditions. These statements allow you to execute different blocks of code depending on whether a condition evaluates to true or false. Here’s the basic syntax for these statements:

php
if (condition1) {
    // Code to be executed if condition1 is true
} elseif (condition2) {
    // Code to be executed if condition2 is true
} else {
    // Code to be executed if both condition1 and condition2 are false
}

Let’s break down the different parts:

  • The if statement checks a condition and executes the block of code inside the curly braces if the condition is true.
  • The elseif statement allows you to check an additional condition if the preceding if or elseif condition(s) evaluate to false. You can have multiple elseif statements to check different conditions.
  • The else statement is optional and serves as a catch-all block of code to be executed if all the preceding conditions are false. It doesn’t require a condition because it only runs when none of the previous conditions evaluate to true.

Here’s an example that demonstrates the usage of these statements:

php
$age = 25;

if ($age < 18) {
    echo "You are underage.";
} elseif ($age >= 18 && $age < 65) {
    echo "You are eligible to vote and work.";
} else {
    echo "You are eligible for retirement benefits.";
}

In this example, the code checks the value of the $age variable and prints a corresponding message based on the condition that evaluates to true.

Note that you can nest if…else statements within each other to create more complex conditions and execution flows. However, it’s important to keep the code readable and maintainable by properly indenting and organizing your statements.

Leave a Reply

Your email address will not be published. Required fields are marked *

GLOTRU Footer
Popular Features
Popular Services/

Website Development & Design

App Development & Design

Graphic Design

Digital Marketing

SEO (Search Engine Optimization)

SMM (Social Media Marketing)

Cyber Security

Company

GLOTRU Founder & CEO : __Azam

Registared : Trade,MSME,etc

Board of Director

Team

About Us

Contact Us

Privacy Policy

Return & Refund Policy

Abuse Policy

Copyright Policy

Cookie Policy

Terms & Conditions

Universal Terms of Service

Disclaimer

Legal

Sponsorships

Investor

Press Releases

Our Investments

Brands

Newsroom

Business

...

_

Digital Millennium Copyright Act
DMCA.com Protection Status

_

Content similarity detection
Protected by Copyscape

_

***ANTI-PIRACY WARNING***

...................................................................................

Follow Us :

...................................................................................

SECURE SERVER : [Legal] [Privacy Policy] [Universal Terms of Service] [Do not sell my personal information]

SITE HOSTED : GLOTRU SECURE SERVER Asian Data Centre [You can host your site][Click Here]

SSL : Server Type : [Cloudflare] Certificate Issued By : [Let's Encrypt] Signature Algorithm : [ECDSA with SHA-384]

SITE BUILD SOFTWARE : Content Management System (CMS) Softwere

_

_

_