0

The following code is from Symfony 2, input string 'folder/file.exe', will output 'file.exe'.

protected function getName($name)
{
    $originalName = str_replace('\\', '/', $name);
    $pos = strrpos($originalName, '/');
    $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);

    return $originalName;
}

However, I can understand everything but the following code structure:

    $var = false === 'something';

Can anyone explain this to me? Thanks!

Edit:Thanks all for helping me, maybe the following code is more clear than the above code:

    $originalName = ((false === $pos) ? $originalName : substr($originalName, $pos + 1));
Eathen Nutt
  • 1,433
  • 5
  • 19
  • 27

3 Answers3

1

It is short form of if else condition

if($a == 1) {
    $value = 1;  
} else {
    $value = 2;
}

Same thing is

$value = $a==1 ? 1 : 2;
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
1

see http://www.php.net/manual/en/language.operators.comparison.php

=== only returns true when the type (string, int etc.) is the same, too

1
$originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);

Expansion above process:

if($pos === false) {
   $originalName = $originalName;
} else {
   $originalName = substr($originalName, $pos + 1);
}

Or

if($pos !== false) {
   $originalName = substr($originalName, $pos + 1);
}
Bora
  • 10,529
  • 5
  • 43
  • 73