background
|
|
Linux
Linux Support
A small area on the internet for Linux Support.
More icon

Linux
Linux Support
A small area on the internet for Linux Support.
More icon
|
|
Up Image
Navigation
Search this Site
Enter your search terms

Site Breadcrumb - You are here
|
PHP from the Ground Up

Page 9 — Comparison and Logical Operators

We've seen how the "=" sign works when we set variables in the scripts we've written so far (as in the code $FavoriteColor = "blue"; and $utterance = "I will smite you!"; ). The equal sign here is what we call the "assignment operator" and is the simplest operator we've got: $a = b means "the variable a is assigned the value b (for the moment)."

But you can squeeze a lot more juice out of your PHP using what are called "comparison operators," "logical operators," and "arithmetic operators." Here's what they are, in tables copied verbatim from PHP.net.

Comparison Operators

These give you the ability to compare whether elements are equal, identical, less than or greater than one another (with some other variations).

Example Name Result
$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only)
$a != $b Not equal TRUE if $a is not equal to $b.
$a <> $b Not equal TRUE if $a is not equal to $b.
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.

Logical Operators

Here you can compare elements using comparisons and, or, and the like.

Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.

Aritmetic Operators

Just what it says — this is basic math.

Example Name Result
$a + $b Addition Sum of $a and $b.
$a - $b Subtraction Difference of $a and $b.
$a * $b Multiplication Product of $a and $b.
$a / $b Division Quotient of $a and $b.
$a % $b Modulus Remainder of $a divided by $b.

next page»