Numeric Comparisons

Use == to Compare Two Numbers for Equality


$a =  4 == 4;      # TRUE
$a =  4 == 2 + 2;  # TRUE
$a =  4 == $b;     # depends on what $b is

Do not confuse == with =

== is for numeric comparison.

= is for assignment.


Use != to Compare Two numbers for Non-Equality


$a =  4 != 4;      # FALSE
$a =  4 != 2 + 2;  # FALSE
$a =  4 != $b;     # depends on what $b is


Use > and < for "Greater than", "Less than"


$a =  4 > 3;      # TRUE
$a =  4 < 3;      # FALSE
$a =  4 > $b;     # depends on what $b is


Use >= and <= for "Greater than or Equal", "Less than or Equal"


$a =  4 >= 3;      # TRUE
$a =  4 >= 4;      # TRUE
$a =  4 <= $b;     # depends on what $b is

Use <=> to Compare Two Numbers


$result = $a <=> $b

$result is

NB: <=> is really useful in conjunction with the sort() function.
<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Thu Oct 9 14:26:02 EDT 2003