PHP If, If....Else , Else...If Statement

This is used while making a decision. If statement executes a code only upon a specific condition is true.

Syntax

if (condition){
 //code will be executed if specified condition is true
}

Example

<?php
$age = 20; // student age
if ($age == 20) {
	echo "You are teen ager";
}
?>

This will produce following result

You are teen ager

If....Else Statement

We can enhance any decision making process by adding an else statement to the if construction. This allows to run one block of code if an expression is true and a different block of code if the expression is false.

Syntax

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

Example

<?php
$age = 21; // student age
if ($age < 21) {
	echo "student age less than 21";
}
else {
	echo "student age greater than 20";
}
?>

This will produce following result

student age greater than 20

Else...If Statement

We can even combine the else statement with another if statement to make as many alternative choices as we like:

Syntax:

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

Example

<?php
$age = 21; //student age
if ($age == 20) {
	echo "You are teen ager";
}
else if ($age > 20 && < 50) {
	echo "You are adult";
} else {
	echo "You are old";
}
?>

This will produce following result

You are adult


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.