What are the PHP operators ? and : called and what do they do?
This is the conditional operator.
$x ? $y : $z
means "if $x
is true, then use $y
; otherwise use $z
".
It also has a short form.
$x ?: $z
means "if $x
is true, then use $x
; otherwise use $z
".
People will tell you that ?:
is "the ternary operator". This is wrong. ?:
is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.
What is the -> PHP operator called?
The official name is "object operator" - T_OBJECT_OPERATOR.
What is the name of this operator: “-> in php?
It is the object operator (known as T_OBJECT_OPERATOR
internally).
What is the name of the :: operator?
::
is the scope resolution operator (you may sometimes find references to Paamayim Nekudotayim
, hebrew for "double colon"). It is used to call static functions of a class, as in
class MyClass {
public static function hi() {
echo "hello, world";
}
}
MyClass::hi();
For more details on classes and objects, refer to the official documentation.
?: operator (the 'Elvis operator') in PHP
It evaluates to the left operand if the left operand is truthy, and the right operand otherwise.
In pseudocode,
foo = bar ?: baz;
roughly resolves to
foo = bar ? bar : baz;
or
if (bar) {
foo = bar;
} else {
foo = baz;
}
with the difference that bar
will only be evaluated once.
You can also use this to do a "self-check" of foo
as demonstrated in the code example you posted:
foo = foo ?: bar;
This will assign bar
to foo
if foo
is null or falsey, else it will leave foo
unchanged.
Some more examples:
<?php
var_dump(5 ?: 0); // 5
var_dump(false ?: 0); // 0
var_dump(null ?: 'foo'); // 'foo'
var_dump(true ?: 123); // true
var_dump('rock' ?: 'roll'); // 'rock'
?>
By the way, it's called the Elvis operator.
What does isset($x) ? $y : $z mean?
That is a Ternary Operator, also called the "conditional expression operator" (thanks Oli Charlesworth). Your code reads like:
if $x is set, use $y, if not use $z
What is the meaning of three dots (...) in PHP?
This is literally called the ...
operator in PHP, but is known as the splat operator from other languages. From a 2014 LornaJane blog post on the feature:
This feature allows you to capture a variable number of arguments to a function, combined with "normal" arguments passed in if you like. It's easiest to see with an example:
function concatenate($transform, ...$strings) {
$string = '';
foreach($strings as $piece) {
$string .= $piece;
}
return($transform($string)); }echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");
(This would print I'D LIKE 6 APPLES
)
The parameters list in the function declaration has the
...
operator in it, and it basically means " ... and everything else should go into$strings
". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $stringsarray
, ready to be used.
What is the name of '->' in PHP?
I mostly call it object access operator.
EDIT: Actually, come to think of it; I usually pronounce it as "dot", since most of us here are more familiar with the dot-notation for accessing objects properties. Since it's clearly not a dot, that's probably not the answer you were looking for.
Related Topics
How to Set Http Header to Utf-8 Using PHP Which Is Valid in W3C Validator
How to Find Out If You'Re Using Https Without $_Server['Https']
PHP - Insert a Variable in an Echo String
Detect Language from String in PHP
MySQL Prepared Statements With a Variable Size Variable List
Storing Files in Database VS File System
PHP Associative Array Duplicate Keys
Turning Multidimensional Array into One-Dimensional Array
Base_Url() Function Not Working in Codeigniter
Get All Permutations of a PHP Array
Warning: MySQLi_Select_Db() Expects Exactly 2 Parameters, 1 Given
How to Fix Error: Laravel.Log Could Not Be Opened
How to Declare a Global Variable in PHP
Finding N-Th Permutation Without Computing Others