Vergleichs-Operatoren erlauben es - wie der Name schon sagt - zwei Werte zu vergleichen. Wenn Sie an Beispielen verschiedener auf Typen bezogener Vergleiche interessiert sind, können Sie sich PHP type comparison tables anschauen.
Beispiel | Name | Ergebnis |
---|---|---|
$a == $b | Gleich | Gibt TRUE zurück, wenn $a gleich $b ist. |
$a === $b | Identisch | Gibt TRUE zurück wenn $a gleich $b ist und beide vom gleichen Typ sind (eingeführt in PHP 4). |
$a != $b | Ungleich | Gibt TRUE zurück, wenn $a nicht gleich $b ist. |
$a <> $b | Ungleich | Gibt TRUE zurück, wenn $a nicht gleich $b ist. |
$a !== $b | Nicht identisch | Gibt TRUE zurück, wenn $a nicht gleich $b ist, oder wenn beide nicht vom gleichen Typ sind (eingeführt in PHP 4). |
$a < $b | Kleiner Als | Gibt TRUE zurück, wenn $a kleiner als $b ist. |
$a > $b | Größer Als | Gibt TRUE zurück, wenn $a größer als $b ist. |
$a <= $b | Kleiner Gleich | Gibt TRUE zurück, wenn $a kleiner oder gleich $b ist. |
$a >= $b | Größer Gleich | Gibt TRUE zurück, wenn $a größer oder gleich $b ist. |
Ein weiter Vergleichs-Operator ist der "?:"- oder Trinitäts-Operator.
<?php
// Beispielanwendung für den Trinitäts-Operator
$action = (empty($_POST['action'])) ? 'standard' : $_POST['action'];
// Obiges ist mit dieser if/else-Anweisung identisch
if (empty($_POST['action'])) {
$action = 'standard';
} else {
$action = $_POST['action'];
}
?>
Der Ausdruck (ausdr1) ? (ausdr2) : (ausdr3) gibt ausdr2 zurück, wenn ausdr1 TRUE zurückgibt und ausdr3, wenn ausdr1 FALSE zurückgibt.
Siehe auch strcasecmp(), strcmp(), Array-Operatoren und den Abschnitt über Typen.
Another conditional operator is the "?:" (or ternary) operator.
Beispiel #1 Assigning a default value
<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
// The above is identical to this if/else statement
if (empty($_POST['action'])) {
$action = 'default';
} else {
$action = $_POST['action'];
}
?>
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.
Hinweis: 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.
Hinweis: It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious:
Beispiel #2 Non-obvious Ternary Behaviour
<?php
// 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.
?>