CopyPastehas never been so tasty!

Ternary and Equality Operators in PHP

by anonymous

  • 0
  • 0
  • 0
76 views

Orignal Source:http://codingvilla.com/ternary-equality-operators-php-single-article-593.aspx

 

Ternary Operator

 

Ternary operator is short hand type of the if and else statement.

 

Syntax:

 

(Condition) ?”message1”,”messge2”;

 

Like if statement its first part is also a condition if it is true then message 1 is printed and else message 2 is printed.

 

Example:

 

Code:

 

<?php

$marks=35;

$marks_result = ($marks > 33) ? 'pass' : 'Fail';

echo "Marks result is   ".$marks_result;

?>

 

Output:

 

Marks result is pass

 

Explanation:

 

On line2 we have declared a $marks variable that contains value 35. On line 3we have checked through ternary operator if marks are greater than 33 so the message “pass” will be printed else fail will be printed. On line 4 we have printed the $marks_result value that is decided on the base of $marks>33 condition.

 

Equality Operators

 

Equality operator are used to check the status of the $var. It checks the equality $var to something like constant, bool, char, string. Equality Operators are used in conditional statements.

 

Code:

 

<?php

$marks=100;

if ($marks == 100) {

    echo "you got 100 markss";           

}

else{

echo"you got no marks";

            }

            echo "<br/>";

?>

 

 

Output:

 

Marks result is pass you got 100 marks

 

Explanation:

 

In the above example we have checked that $marks is equal to 100 if so then output is “Marks result is pass you have got 100 marks” else "you have got no marks".

 

Code:

 

<?php

$books=100;

$marks=100;

if ($marks == $books) {

    print "Your marks and Books you learn are  equal";

}

else{

echo "no books";

}

echo "<br/>";

?>

 

Output:

 

Your marks and Books you learn are equal no books

 

Explanation:

 

In the above example we check that value $books is equal to $marks if so then output is “Your marks and Books you learn are equal” else " no books ".

 

Code:

 

<?php

$name="Tahreem";

if($name="Tahreem"){

            echo "your name  $name";

            }

            else{

                        echo "what your name";

                        }

                        echo "<br/>";

?>

 

Output:

 

your name Tahreem

 

 

Explanation:

 

In the above example we have checked that $name value “Tahreem” if so output is your name Tahreem else what is your name.

 

Code:

 

 

<?php

$Blood='A+';

 

if ($Blood == 'A+') {

    echo "Your Blood Group is A+";

}

else{

            echo "what is your blood group";

           

            }

            echo "<br/>";

?>

 

Output:

 

Your Blood Group is A+

 

Explanation:

 

In the above example we have checked that char type value $Blood  is equal to ‘A+’ if so then output is “Your Blood Group is A+” else " what is your blood group ".

 

Add A Comment: