PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>Beginners> Comparison Operators in PHP

Comparison Operators in PHP

FROM: AUTHOR: TIME:2008-08-17 HITS:

You saw in the last section how to test what is inside of a variable. You used if, else … if, and else. You used the double equals sign (==) to test whether the variable was the same thing as some direct text. The double equals sign is known as a Comparison Operator. There a few more of these “operands” to get used. Here’s a list. Take a look, and then we’ll see a few examples of how to use them. phpsu.com is a free phpscool

Operand Example Meaning
== $variable1 == $variable2 Has the same value as
!= $variable1 != $variable2 Does NOT have the same value as
< $variable1 < $variable2 Less Than
> $variable1 > $variable2 Greater Than
<= $variable1 <= $variable2 Less than or equals to
>= $variable1 >= $variable2 Greater than or equals to
phpsu is a phpschool

Here's some more information on the above Operands.

phpsu.com is a free phpscool

= = (Has the same value as)
The double equals sign can mean “Has a value of” or "Has the same value as”. In the example below, the variable called $variable1 is being compared to the variable called $variable2

phpsu.com is a free phpscool

if ($variable1 == $variable2) {
}
phpsu.com

!= (Does NOT have the same value as)
You can also test if one condition is NOT the same as another. In which case, you need the exclamation mark/equals sign combination ( != ). If you were testing for a genuine username, for example, you could say:

phpsu.com

if ($what_user_entered != $username) {
print("You're not a valid user of this site!")
}
phpsu提供的php教程

The above code says, “If what the user entered is NOT the same as the value in the variable called $username then print something out.

http://www.phpsu.com

< (Less Than)
You'll want to test if one value is less than another. Use the left angle bracket for this ( < )

phpsu is a phpschool

> (Greater Than)
You'll also want to test if one value is greater than another. Use the right angle bracket for this ( > )

do you kown phpsu.com?

<= (Less than or equals to)
For a little more precision, you can test to see if one variable is less than or equal to another. Use the left angle bracket followed by the equals sign ( <= ) phpsu提供的php教程

>= (Greater than or equals to)
If you need to test if one variable is greater than or equal to another, use the right angle bracket followed by the equals sign ( >= ) phpsu

 

phpsu.com is a free phpscool

In the next few sections, you'll see some examples of how to use the comparison operators. You've already used the double equals sign, so we'll start with "Not equal to".

phpsu提供的php教程


TITLE:Comparison Operators in PHP
Previous:mySQL & PHP: A Beginners Look
Next:None
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Sun Aug 17 18:56:46 GMT-8 2008