tags:

views:

113

answers:

8

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

+10  A: 

It's the equivalent of

if (null === $length)
  $length = strlen($data);
else
  $length = (int)$length;
Aistina
+1 first come, first serve
Gordon
Thanks a lot. :)
MEM
+8  A: 

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 ;
Felix Kling
+1 for the example with added parentheses, it really is much clearer.
Aistina
Thank you, it really makes a difference those parentheses. :)
MEM
A: 

This is basically the same as

if ($length === null)
{
    $length = strlen($data)
}
else
{
    $length = (int)$length;
}
Denis 'Alpheus' Čahuk
Thanks. :) Why to I start feeling like a dummy? :s ;)
MEM
A: 

It means

if (null === $length) {
   $length = strlen($data);
} else {
   $length = (int)$length ;
}
Mark Baker
A: 
$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.

SorcyCat
A: 

It's the same as $length = (null === $length ? strlen($data) : (int)$length );

burningstar4
Felix Kling parents? Thanks. :)
MEM
A: 

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) {.

Ivo Wetzel
Thanks. :) I didn't know about null===$length versus $length===null. :)
MEM
A: 

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

Teodor Pripoae