Thursday, 28 December 2017

If statement in PHP made get it

Howdy my kin, today subject is interesting like no other php put forth basic with if expression, without much talk,  if statement are Control Structures in PHP usein making decisions.

if statement control block

if(condition)
{
statement
}

for example

1. if ($x > 0)
{

$y = 5;

}

Code explained: if $x as a value is greater than 0 as a value then assign 5 to y variable

Now, let take a look at another sample code to broaden our understanding more on this context.

2. if($username==$logusername)
{
    echo "Username Correct!";
}
or
if($access==0)
{
    echo "Access denied!";
}

    This time we will be dealing with if else statement

3. if ($a)
{
// print if $a is true or not
print($b);
$b++;
}
else
print($c);


4. if ($a > $b)
print "a is greater than b";
elseif ($a == $b) // use "elseif" or "else if"
print "a is equal to b";
else
print "a is less than b";

5. switch ($vehicle_type) { // works for integers, floats, or strings
case "car":
$car++;
break;

case "truck":
$truck++;
 break;

case "suv":
$suv++;
break;

default:
$other++;
}

We are going to talk more on switch later on...

No comments:

Post a Comment