Home of web learners

www.webn3rd.com

Sometimes you see some condition in real life,like if your friends come in the afternoon you will go shopping with him

So here you see that there is a condition,you can use this type of things in php

PHP has if else to use condtion

lets see an example:

<?php
if(5 > 2){
echo "ok";
}
?>

output:

ok

here you see that we use condition in bracket () after if,so the conditon is true here,that means inside {} bracket code is going to execute

if the condition inside () bracket is not true then the code is not going to executes and no output comes

So,if have many condition,how to use these

if else for two conditional use

if elseif else for more than two conditional use,you can use more than one else ife,like 20 else if or 30 else if,what you need

Example for if else structure:

<?php
if(2 > 5){
echo "ok";
}else{
echo "not ok";
}
?>

output:

not ok

here,you see that condition in if is not true,so else code is going to executes and the thing else do not have condition,its works at last if condition is not true

now,lets check for if else if else structure

Its better to use else if,is there more condtion than one,then you can use more else if,its like if,better to use only one if,then all else if

example:

<?php
if(2 > 5){
echo "2 is greater than 5";}
else if(2 > 3){
echo "2 is greater than 3";}
else if(2 > 1){
echo "2 is greater than 1";}
else{
echo "nothing true";
}
?>

output:

2 is greater than 1

Here,first and second condition is not true,but third condition is true and we don't need else condition to run as one condition of them is true

webn3rd.com

About webn3rd