I'm aware of ternary operator, more or less. But I'm unable to read this line.
$length = null === $length ? strlen($data) : (int)$length ;
What does $length = null === $length means?
Thanks a lot, MEM
I'm aware of ternary operator, more or less. But I'm unable to read this line.
$length = null === $length ? strlen($data) : (int)$length ;
What does $length = null === $length means?
Thanks a lot, MEM
It's the equivalent of
if (null === $length)
$length = strlen($data);
else
$length = (int)$length;
It means:
If the value of $length is null, assign strlen($data) to $length, otherwise (int)$length.
It is easier to understand if parenthesis are put at the right place:
$length = (null === $length) ? strlen($data) : (int)$length ;
This is basically the same as
if ($length === null)
{
$length = strlen($data)
}
else
{
$length = (int)$length;
}
It means
if (null === $length) {
$length = strlen($data);
} else {
$length = (int)$length ;
}
$length = (null === $length ? strlen($data) : (int)$length ) ;
if (null === $length) {
$length = strlen($data);
} else {
$length = (int)$length;
}
The === means that $length must be exactly null. See PHP Comparison Operators.
It's the same as $length = (null === $length ? strlen($data) : (int)$length );
Adding a couple of parenthesis should make it clear:
$length = (null === $length ? strlen($data) : (int)$length);
Also the use of null === $length instead of $length === null is just coding style, mainly used to ne against stupid compilers that don't warn you about something like if (foo = null) { instead of if (foo == null) {.
a === b means a is identical to b and they are of the same type.
So if $length === null then $length = strlen($data) else $length