CopyPastehas never been so tasty!

Conditional Operators and Statements in PHP

by anonymous

  • 0
  • 0
  • 0
69 views

orignal Source:http://codingvilla.com/conditional-operators-statements-php-single-article-595.aspx

 

Conditional Operators

 

Code:

 

<?php

$var1=40;

$var2=20;

$result=($var1>$var2)? $var1:$var2;

echo "The value of result is".$result;

?>

 

Output:

 

The value of result is40

 

Explanation

 

Conditional operator check the condition if the condition is true than $var1=40 is displayed else part $var=20 is displayed.

 

In the above code on line 4 we have checked if $var1>$var2 true case therefore we will display $var1 and on false case $var2 will be displayed.

 

Statements in php

 

Statements are the message that we displays on the screen when certain condition is true or false.

 

Code:

 

<?php

$marks = 34;

if ($marks < 33) {

print "You are fail";

} else {

print "you are pass";

}

echo "<br/>";

 

if ($marks >= 33 && $marks <90) {

print "You are in the range of 33 to 90";

} else {

print "You are not in the range of 33 to 90";

}

echo "<br/>";

if ($marks>= 60) {

print "you got 60+ marks";

} else {

print "yours marks are less than 60";

}

echo "<br/>";

?>

 

 

Output:

 

The value of result is 40 you are pass

You are in the range of 33 to 90

yours marks are less than 60

 

 

Explanation

 

In the above code we have declared a $marks variable on line 2. We have checked if the value is less than 33 then print statement “You are fail” and on false print statement "you are pass". On line 8 we have checked if marks is greater or equal 33 and less than 90 then print statement "You are in the range of 33 to 90".On false print statement “You are not in the range of 33 to 90”.Online 14 we have checked marks is greater or equal 60 then the output statement will be you have got 60+ marks and on false the output statement will be “yours marks are less than 60”.

 

Add A Comment: