Main | School

Variable comparison methods.

Comparison methods are important to nearly all PHP programs and all other programmers. Comparison is essentaily taking 2 values and comparing them together. The operator returns TRUE or FALSE depending on the values. This is extremly useful in combination with IF and WHILE functions, that can control what code is executed on your website.

PHP includes comparison operators fonund in most programming languages and a few others. The basic operaters are <, >, !=, ==, <=, and >=.

Greater than: > and Less than: <;

This will test if the first variable is larger than the second. For instance, 34 > 4 would return TRUE, while 6 > 10 would return FALSE. Comparison operators are not limited to numbers, they can compare characters too. In comparting a single characters, they will be compaared according to alphabetical order, a being the smallest and Z being the largest. When strings are compared, the sum of all the characters is compared. When comparing strings, you may want to use string functions.

Equals: == and Not equals: !=

These operators will determine of two variables are of the same value. An alternative sympol for != is <>, which is also used in SQL and other loder languages.

Gearter than or equal to: >=

Less than or equal to: <= Much like the greater than or less than operators, but also check to see if the two values are equal.

Identical === and Notidentical !==

These operators are similar to the equals operator, but it will also check to see if the two variables are the same type. This is useful when a variable may contain any number of different kinds of variables, and you need to make sure that a value is not only a specific value, but that it doesn't have anything else in it.

References: www.php.net

Contact Me | Policies | Colophon

© 2009, Jonathan Dunstan, All Rights Reserved.