php if-else statement

PHP if Conditional Statement

Conditional statement in php used to take decision on based the conditions is true or false.

Simple if statement

Simple if statement is used when we have only one condition and the code will be executed only and only if the condition is true. There is not code for execution the if condition is not true.

PHP – simple if condition

<?php

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

?>

In above if condition we have some code for execution but it will only execute the condition is true. If the condition is not true there will be nothing to execution.

lets some php example to understood more about simple if statement.

simple if statement example

<?php

$a=10;
$b=7;

if($a>$b)
{
echo “a is greater than b”;
}

?>

The output is:
a is grater than b

in above php example we have two variables $a and $b with values 10 and 7 respectively.
Here, the if condition is true so code will be executed and result will b “a is grater than b”.

if – else statement in php

In if-else statement execute some code the if condition is true or execute another code the if condition is false.
Here we can write two answer for one condition if the condition is true or false.

PHP if-else statement

<?php

if(condition)
{
// condition is true
}
else
{
// condition is false
}

?>

 

Example of if-else statement in php

<?php

$uname=”meera”;
$upass=”ram”;

if($uname==”meera” && $upass==”ram”)
{
echo “welcome to system”;
}
else
{
echo “Error, Please try again”;
}

?>

The output is:
welcome to system

In above php example we have two variables with values $uname and $upass. Here, in if condition we check the $uname and $upass values is right or wrong. The if condition is true then print “welcome to system” message and if false then print “Error, Please try agin” message.

if-else example

<?php

$a=10;
$b=17;

if($a>$b)
{
echo “a is greater than b”;
}
else
{
echo “a is less than b”;
}

?>

The output is:
a is less than b

In above example value $a is 10 and $b is 17, so if condition is false so execute else code and print “a is less than b” message in output.

Leave a Reply

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

3 + 1 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">