Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
PHP7 also brings us some new operators. The first one we’re going to explore is the spaceship operator. With a name like that, who doesn’t want to use it? The Null Coalesce Operator, is effectively the fabled if-set-or. It will return the left operand if it is not NULL, otherwise it will return the right.
Example | Name | Result |
---|---|---|
$a == $b | Equal |
TRUE if $a is equal to $b after type juggling. |
$a === $b | Identical |
TRUE if $a is equal to $b, and they are of the same
type.
|
$a != $b | Not equal |
TRUE if $a is not equal to $b after type juggling. |
$a <> $b | Not equal |
TRUE if $a is not equal to $b after type juggling. |
$a !== $b | Not identical |
TRUE if $a is not equal to $b, or they are not of the same
type.
|
$a < $b | Less than |
TRUE if $a is strictly less than $b. |
$a > $b | Greater than |
TRUE if $a is strictly greater than $b. |
$a <= $b | Less than or equal to |
TRUE if $a is less than or equal to $b. |
$a >= $b | Greater than or equal to |
TRUE if $a is greater than or equal to $b. |
$a <=> $b | Spaceship | An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7. |
$a ?? $b ?? $c | Null coalescing |
The first operand from left to right that exists and is not NULL .
NULL if no values are defined and not NULL . Available as of PHP 7.
|
Documentation
Comparison Operators
Spaceship Operator RFC
Null Coalesce Operator RFC
PHP7 also brings us some new operators.
0:01
The first one we're going to
explore is the space ship operator.
0:04
With a name like that,
who doesn't want to use it?
0:08
The space ship operator, or combined
comparison operator, is a nice addition
0:11
to the language, complementing the greater
than and the less than operators.
0:16
The spaceship operator is put together
using three individual operators
0:21
less than, equal and greater than.
0:26
Essentially what it does is check
each operator individually.
0:29
First, less than.
0:33
If the value of the left is
less than a value on the right.
0:35
The Spaceship Operator
will return minus 1.
0:39
If not,
0:42
it will move to test if the value on the
left is equal to the value on the right.
0:43
If so, it will return 0.
0:50
If not, it will move to the final test,
0:52
if the value on the left is greater
than the value on the right.
0:56
Which if the other two haven't passed this
one must be true and it will return 1.
1:01
The most common uses for
this operator is in sorting.
1:06
Another new operator the null coalesce
operator is effectively the fabled if,
1:11
set, or.
1:16
It will return the left
operand if it is not null.
1:17
Otherwise it will return the right.
1:22
The important thing is that it will not
raise unnoticed if the left operand
1:24
is a nonexistence variable.
1:29
For example,
1:31
name equals the variable first name
double question marks the string Guest.
1:33
If the variable first name is Seth and
it is not null.
1:39
It will assign that value
to the variable name or
1:44
else it will assign Guest
to the variable name.
1:48
Before PHP7,
you could write something like this.
1:52
If not empty first name, name equals
first name else name equals Guest.
1:56
What makes this even more powerful,
is that you can stack these.
2:03
This operation will check
each item from left to right.
2:07
And when it finds one that is not null,
it will use that value.
2:11
This operator looks explicitly for
null, or does not exist.
2:16
It will pick up an empty string.
2:21
You need to sign up for Treehouse in order to download course files.
Sign up