Question

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));
Was it helpful?

Solution

$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);
}

OTHER TIPS

It is short form of if else condition

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

Same thing is

$value = $a==1 ? 1 : 2;

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top