cramerz logo

PHP Conditional Statements Using If Else


Conditional statements are used when we want to perform an action based on a condition. For instance, I can check if its raining then stay home, else lets go out. In this example condition is to check the rain. Conditional statements have a body structure ( {} ). PHP keywords for condional statements are if, else, else if.

if/else

Decisions in PHP take the following form:

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

The else part of the above is optional. Condition is any expression that can be evaluated to true or false. PHP treats 0 and the empty string as false, and everything else as true. Here is some example code:

$name = "Bill";   // ...   if ($name == "Bill") { echo "Hi Bill!"; } else { echo "Hello person-who-is-not-Bill"; }

ifs can be chained together using elseif :

  if ($width > $height) { echo "The object is wider than it is high"; } elseif ($width == $height) { echo "The object is square"; } else { echo "The object is higher than it is wide"; }

 

An Example:

<?php

//decleare a variable and assign it a value

$day = "wed";

if($day == "wed")

{

echo "Today is wednesday";

}

else

{

echo "Do you know what's the day today?";

}

?>



all rights reseverd by Cramerz.com©. web design & web development by DotPeak. hosting by HostBay
all contents on cramerz are the property of Cramerz.com. web design courses