You are here: Daniel Dumas » Blog, PHP »

PHP Operators : Using Comparison Operators

PHP Operators : Using Comparison Operators

To compare php variables to determine whether they are the same or different.. you can use PHP Comparison Operators. See below for a list of examples..

I. Define the Variables

<?php
$num1 = 9;
$num2 = 9;
$num3 = 22;
?>

A. The Less Than Operator

<?php
//Returns true if variable in the left side is less than the right..
$res = ($num1 < $num3);
//returns true..
?>

B. The Greater Than Operator

<?php
//Returns true if the variable in left side is greater that the right..
$res = ($num3 > $num1);
//returns true..
?>

C. The Less Than or Equal-to Operator

<?php
//returns true if variable on left is less than or equal to right..
$res = ($num3 <= $num1);
//returns false
?>

D. The Greater Than or Equal-to Operator

<?php
//returns true if variable on left is greater than or equal to right..
$res = ($num3 >= $num2);
//returns true..
?>

E. The Equality Operator

<?php
//returns true if both variables are equal to each other
$res = ($num1 == $num2);
//returns true..
?>

F. The Not Equal-to Operator

<?php
//returns true if the variable on left is not equal to right
$res = ($num1 != $num2);
//returns false..
?>

G. The Inequality Operator

<?php
//returns true if the variable on left is not equal to right..
$res = ($num1 <> $num2);
//returns false..
?>

Discussion

  1. wii gamerz says:

    Wow so complicated.. i never study bout those… what is comparison operator for?

    wii gamerz’s last blog post..What is the Wii Zapper?

  2. You could include the double equality operator i.e == which tests for equality and explain about that too.

  3. Awesome blog! Just discovered it today… I will definitely be back. I’ve been learning MODX CMS and am having to also learn PhP at the same time.

    I love the internet and so glad peeps like you create useful sites!

    Ciao!

  4. Albert says:

    I great site for newbies on PHP.

    The best thing to learn PHP s 2 study hard

    Study, practice and research…

  5. I have much respect for programmers. Im a bit of a control freak so i like to do as much myself… with everything. Well, why not program the code for my site – NOT. Well, at least you give me a place to start.

Leave a Reply





Spam protection by WP Captcha-Free