Difference between revisions of "PHP/operator"
< PHP
Jump to navigation
Jump to search
Line 25: | Line 25: | ||
| <=> || spaceship || <syntaxhighlight lang=php inline>$a <=> $b</syntaxhighlight> || An int less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. || common | | <=> || spaceship || <syntaxhighlight lang=php inline>$a <=> $b</syntaxhighlight> || An int less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. || common | ||
|- | |- | ||
− | | ?: || ternary | + | | ?: || [https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary ternary] || <syntaxhighlight lang=php inline>$A ? $B : $C</syntaxhighlight> || "if $A is true, then $B, else $C" || less common || 2024-07-21 Currently testing to see if this can be used as an if/then replacement when there's no return value. |
|- | |- | ||
− | | ?? || [https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce null coalescing] || <syntaxhighlight lang=php inline>$A ?? $B</syntaxhighlight> || "$A if it isn't NULL, else $B" || rare | + | | ?? || [https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce null coalescing] || <syntaxhighlight lang=php inline>$A ?? $B</syntaxhighlight> || "$A if it isn't NULL, else $B" || rare |
+ | |- | ||
+ | | ??= || [https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce null coalescing] || <syntaxhighlight lang=php inline>$A ??= $B</syntaxhighlight> || "if $A is NULL, set it to $B"<br>equivalent to <syntaxhighlight lang=php inline>$A = $A ?? $B</syntaxhighlight> || rare | ||
|- | |- | ||
| instanceof || [https://www.php.net/manual/en/language.operators.type.php type comparison] || <syntaxhighlight lang=php inline>$A instanceof $B</syntaxhighlight> || TRUE if $A is a class (or object of a class) that inherits from class $B || nonstandard || see {{l/same|inheritance}} | | instanceof || [https://www.php.net/manual/en/language.operators.type.php type comparison] || <syntaxhighlight lang=php inline>$A instanceof $B</syntaxhighlight> || TRUE if $A is a class (or object of a class) that inherits from class $B || nonstandard || see {{l/same|inheritance}} |
Latest revision as of 16:23, 21 July 2024
List
(very incomplete; mainly to document the more obscure ones)
operator | name | example | meaning | familiarity | docs |
---|---|---|---|---|---|
== | Equal | $a == $b |
true if $a is equal to $b after type juggling. | common | Comparison Operators |
=== | Identical | $a === $b |
true if $a is equal to $b, and they are of the same type. | common-ish | Comparison Operators |
!= | Not equal | $a != $b |
true if $a is not equal to $b after type juggling. | common | Comparison Operators |
<> | Not equal | $a <> $b |
true if $a is not equal to $b after type juggling. | common | |
!== | Not identical | $a !== $b |
true if $a is not equal to $b, or they are not of the same type. | common | |
< | Less than | $a < $b |
true if $a is strictly less than $b. | common | |
> | Greater than | $a > $b |
true if $a is strictly greater than $b. | common | |
<= | Less than or equal to | $a <= $b |
true if $a is less than or equal to $b. | common | |
>= | Greater than or equal to | $a >= $b |
true if $a is greater than or equal to $b. | common | |
<=> | spaceship | $a <=> $b |
An int less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. | common | |
?: | ternary | $A ? $B : $C |
"if $A is true, then $B, else $C" | less common | 2024-07-21 Currently testing to see if this can be used as an if/then replacement when there's no return value. |
?? | null coalescing | $A ?? $B |
"$A if it isn't NULL, else $B" | rare | |
??= | null coalescing | $A ??= $B |
"if $A is NULL, set it to $B" equivalent to $A = $A ?? $B |
rare | |
instanceof | type comparison | $A instanceof $B |
TRUE if $A is a class (or object of a class) that inherits from class $B | nonstandard | see inheritance |
Docs
- Operators
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Error Control Operators: the
@
operator - Execution Operators: the
``
(backtick) operator - Incrementing/Decrementing Operators
- Logical Operators
- String Operators
- Array Operators
- Type Operators
- Operator Precedence: if it matters, use parentheses to make it unambiguous; precedence rules can change.