PHP -Building Blocks

PHP -Building Blocks

php training in gurgaon
PHP -Building Blocks

If –else statements

if…else statement – executes set of codes if a condition is true and another code if the condition is false

Within a statement, if a certain condition is true, then something happens. Otherwise, something else happens. We can continue adding else statements as many times as we need.

So, within PHP statement, we first define the variable and then set up the conditional statements like this:

 

Syntax

if (condition)

code to be executed if condition is true;

elseif (condition)

code to be executed if condition is true;

else

code to be executed if condition is false;

Example

 

<?php

$avg = 50

if ($avg>60)

{

echo “You have a good score”;

}

else

{

echo “Score is less than average”;

}

?>

 

Example 2:

<?php

$status = 1;

if ($status== 1)

{

print (“<img src =images/right.jpg>”);

}

else

{

print (“<img src =images/cross.jpg>”);

}

?>

 

The variable called $status has been assigned a value of 1.The first line of if statement tests to see what is inside of the variable called $status. It’s testing to see whether this variable has a value of 1.

 

Example 3:

<html>

<head>

<title>Days in current month</title>

</head>

<body>

<?php

$month=date(“n”);

if ($month==1 || $month==3 || $month==5 || $month==7 || $month==8 || $month==10 || $month==12 )

{

echo “31 Days”;

}

else if ($month==2)

{

echo “28 or 29 Days”;

}

else

{

echo “30 Days”;

}

?>

</body>

</html>

 

We get the value for variable $month by tapping in to built-in date functions. date(“n”) returns a numeric value of your current month as set in your server, such as 1 for January, 2 for February, and so on.

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/07/31/php-building-blocks/
YouTube31
YouTube