Posted By:
9 Mar, 2014 12:25 am PST

How to write if---else statement block in PHP?

 How to write if---else statement block in PHP?

This is variation of simple if control flow statements. In this if condition evaluated to be true, then if part of block gets executed otherwise else part of block gets executed.

Syntax:-

If(condition)

{

//write your code here, if condition is true

}

{

//write your code here, if condition is false

}

 

 
 

<?PHP

echo " <h2 > " ;

echo " To check number is even or odd " ;

echo " <br > < br > " ;

$a = rand (1 ,100) ;

// rand ( int min , int max ) this function returns a pseudo - random integer between

// “min " and " max " value .

echo " a ={ $a } <br > < br > " ;

if ( $a % 2 == 0 ) // condition

 

{

echo " { $a } is even number " ; // statement

 

}

else

{

echo " { $a } is odd number " ; // statement

 

}

 

?>

 

IfElseStatement.php

IfElseStatement.php

 

 

 

 

 

 

 

 

 


If-else output



How to use if-else statement in PHP?

 

In PHP we have the following conditional statements:

  • if statement 
  • if...else statement 
  • if...elseif....else statement

 

Syntax:

if (condition) {   

  //code condition if statement is true }

else {

    //code condition if statement is false }

 

//To print condition according to if-else statement in PHP

 

<!DOCTYPE html>

<html>

<body>

<?php

$num1 = 7;

$num2 = 2;

if($num1 > $num2){

    echo $num1;

}

else{

    echo $num2;

}

?>  

</body>

</html>

 

/*Output of the above program on your browser is:

7   

*/

Attachment: ifelse.php
Was it useful?  




Please login to reply to this problem.