Tuesday, March 30, 2010

Comparison Operators

Comparison operators, as their name implies, allow you to compare two values.

ExampleNameResult
$a == $bEqualTRUE if $a is equal to $b.
$a === $bIdentical TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only)
$a != $bNot equalTRUE if $a is not equal to $b.
$a <> $bNot equalTRUE if $a is not equal to $b.
$a !== $bNot identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
$a < $bLess thanTRUE if $a is strictly less than $b.
$a > $bGreater thanTRUE if $a is strictly greater than $b.
$a <= $bLess than or equal to TRUE if $a is less than or equal to $b.
$a >= $bGreater than or equal to TRUE if $a is greater than or equal to $b.

Another conditional operator is the "?:" (or ternary) operator, which operates as in C and many other languages.

(expr1) ? (expr2) : (expr3);

This expression evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
Comparison with Various Types:

Type of Operand 1 Type of Operand 2 Result
null or string string Convert NULL to "", numerical or lexical comparison
bool or null anything Convert to bool , FALSE < TRUE
object object Built-in classes can define its own comparison, different classes are uncomparable, same class - compare properties the same way as arrays (PHP 4), PHP 5 has its own explanation
string , resource or number string , resource or number Translate strings and resources to numbers, usual math
array array Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by value (see following example)
array anything array is always greater
object anything object is always greater



Example #1 Transcription of standard array comparison


// Arrays are compared like this with standard comparison operators
function standard_array_compare($op1, $op2)
{
if (count($op1) <> count($op2)) {
return 1; // $op1 > $op2
}
foreach ($op1 as $key => $val) {
if (!array_key_exists($key, $op2)) {
return null; // uncomparable
} elseif ($val < $op2[$key]) { return -1; } elseif ($val > $op2[$key]) {
return 1;
}
}
return 0; // $op1 == $op2
}
?>

Ternary Operator:

Another conditional operator is the "?:" (or ternary) operator.

Example #2 Assigning a default value



The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

Note: Please note that the ternary operator is a statement, and that it doesn't evaluate to a variable, but to the result of a statement. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.

Example #3 Non-obvious Ternary Behaviour


// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');

// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right

// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');

// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>

Comparison Operators (Transact-SQL)

Comparison operators test whether two expressions are the same. Comparison operators can be used on all expressions except expressions of the text, ntext, or image data types. The following table lists the Transact-SQL comparison operators.

Operator Meaning

= (Equals)

Equal to

> (Greater Than)

Greater than

< (Less Than)

Less than

>= (Greater Than or Equal To)

Greater than or equal to

<= (Less Than or Equal To)

Less than or equal to

<> (Not Equal To)

Not equal to

!= (Not Equal To)

Not equal to (not ISO standard)

!< (Not Less Than)

Not less than (not ISO standard)

!> (Not Greater Than)

Not greater than (not ISO standard)

No comments:

Post a Comment