pTutorial

New way to learn PHP

PHP 5 If - else

If statement

If statement is used to execute a particular area of code if and only if the condition is true otherwise code did not executed in given block.

In Technical Term If the expression inside the parentheses evaluates to true, the code between the braces run. If the expression evaluates to false, the code between the braces is skipped.


Flow chart

flow chart of if

Syntax

If (condition)
Only one line of code executes if and only condition is true;

This type of if statement is known as inline if.

.

If braces are not there, then only one line executes according to the condition, If the condition is true, executes one line otherwise skip one line, Rest of code will execute.

Another Syntax PHP if

if (condition) 
{
These line of code will be execute.
echo "hello"; }

If Example

<?php

$a="john";
$b="Bob";
if ($a==$b) 
echo "Both string are equal";
echo "if block not executed";

?>

In the above example, condition is not true so that it skip one line of code. Rest of the code will execute properly.

PHP conditional statement (if - else)

Whenever the if statement return false then the else block will be executed. The following example explains the concept of if else block.

Syntax

If (condition)
Code to be executed if return true;
else
Code to be executed if return false;

If else example

<?php

$var1="php";
$var2="java";
if ($var1 == $var2)
echo "Both the strings are equal";
else
echo "Both the strings are not equal";

?>

In the above example, the condition is not true so that it skips the one line just after the "if statement" but "else statement" must be executed.


Executing multiple line

You could use braces to execute multiple lines of code, you can put any numbers of line in between braces following example, explain the concept of braces in if or else block.

If (condition)
{
code block to be executed if return true;
echo "hello";
}
else
{
code block to be executed if return false;
echo "hello";
}

Multiple Line example

<?php

$marks=80;
if ($marks >= 60 ) 
	{
		echo "you are passed"."
"; echo "your marks= ".$marks."
"; echo "First division"; } else { echo "Failed"; } ?>

Sometimes an application needs to execute multiple-statement in a single true condition, braces are used to execute multiple statement.

In this example, the condition is true, so all the statement in between "if block" will be executed.

PHP Else if statement

  • Else-if statement can be used to conduct a series of condition check.
  • Only execute the code, where the condition met first.

Consider the following code

<?php

$a=(rand(1,50));//its generate random number

if ($a<="10") 
{
echo "Yoy are baby"."
"; echo "your age are :".$a."
"; } elseif($a<="20" && $a > "10" ) { echo "you are youg boy"."
"; echo "your age are :".$a."
"; } elseif($a<="35" && $a > "20" ) { echo "you are middle age man"."
"; echo "your age are :".$a."
"; } else { echo "you are old man"."
"; echo "your age are :".$a."
"; } ?>

In above the example rand function generate a random number in between 1 to 50, one condition will be executed according to the generated number.

PHP Nested if else

If we write an entire if else construct within the if block or else block. This is called nesting of if else. The second if-else is nested in the first else statement.

Consider the following code

<?php

$a=(rand(1,30));//its generate random number
	if ($a<="10") 
	{
		if ($a=="5") 
		{	
		echo "Yoy win"."
"; echo "your age are :".$a."
"; } else { echo "Yoy loose"."
"; echo "your age are :".$a."
"; } } else { echo "you are man"."
"; echo "your age are :".$a."
"; } ?>

QUOTES OF THE DAY

Wonder is the first cause of philosophy.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER