MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

PHP 7 Fundamental Tutorial for Beginners – If, Else and Elseif Conditional Statements

PHP Conditional Statements

Like most programming languages, PHP also allows you to write code that performs different actions based on the results of logical or comparative test conditions at run time. This means you can create test conditions in the form of expressions that evaluates to either true or false and based on these results you can perform certain actions.

There are several statements in PHP that you can use to make decisions:

  • The if statement
  • The if…else statement
  • The if…elseif….else statement
  • The switch…case statement
Overview of PHP conditional statements: if, if...else, if...elseif...else and switch...case

PHP – The if Statement

The if the statement is used to execute a block of code only if the specified condition evaluates to true. This is the simplest of PHP’s conditional statements and can be written like:

Syntax:

if (condition) {
code to be executed if condition is true;
}

PHP if statement syntax running a code block only when the condition is true

The following example will output “Have a nice weekend!” if the current day is Friday:

Example:-

PHP if statement example printing Have a nice weekend! when the day is Friday

The if…else Statement

You can enhance the decision making process by providing an alternative choice through adding an else statement to the if statement. The if…else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false. It can be written, like this:

Syntax:-

if(condition){
// Code to be executed if condition is true
} else{
// Code to be executed if condition is false
}

PHP if...else example printing Have a nice weekend! on Friday, otherwise Have a nice day!

The following example will output “Have a nice weekend!” if the current day is Friday, otherwise it will output “Have a nice day!”

Example=>

The if…elseif…else Statement

The if…elseif…else a special statement that is used to combine multiple if…else statements.

Syntax:

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

PHP if...elseif...else example that outputs Please Enter Valid Percentage

The following example will output “Please Enter Valid Percentage”
Example=>

you will learn about PHP switch-case statement in the next Blog

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Encryption using RSA public and Private Key using PHP

PHP's OpenSSL extension can create an RSA key pair, encrypt data with the public key and decrypt it with the private key. This page explains the gist…

Read More

Encryption using Public key and Private key in PHP using mySql database

This tutorial shows how to generate an RSA key pair with PHP's OpenSSL extension, encrypt form input with the public key, store the ciphertext in MySQL, and…

Read More

How to see number of emails sent using smtp in laravel

Laravel does not count or log SMTP emails by default, so a grep will only return a real number once your application writes one log line for…

Read More

What are the Authentication type in phpmyadmin which for config.inc.php

phpMyAdmin supports four authentication types, set per server with $cfg['Servers'][$i]['auth_type'] in config.inc.php: cookie (the default), config, http and signon. This page explains how each one works and…

Read More

What is opcache and Install / Configure in Linux

OPcache is PHP's built-in bytecode cache. It keeps compiled scripts in shared memory so PHP doesn't have to parse and compile the same files on every request….

Read More

PHP Code for Godaddy VPS Server SMTP Email Relay

This page shows how to send email from a GoDaddy VPS through GoDaddy's SMTP relay on port 25 with plain PHP sockets and no extra libraries. Below…

Read More