Strange behavior of the if statement: the else block is always executed

People, I doubt whether I am here or my computer is slower.

I have the following piece of code:

class Whatever
{
    ...

    private function requireFile($filePath)
    {
        if(is_array($filePath))
            foreach($filePath as $singleFilePath)
                if($this->requireFile($singleFilePath))
                    break;
        elseif(($filePath = stream_resolve_include_path($filePath = $filePath . '.php')) !== false)
            return require_once $filePath;
    }
}

      

Apart from the ugly code, it doesn't work as expected. The idea here is for this method to take both a string and an array of strings as a parameter, and in the case of an array, recursively process each of the strings.

What happens when, when providing an array as a parameter, both the if and are executed elseif

, and thus make the PHP parser scream that it encounters an array to convert strings to string elseif

.

I don't understand why adding parentheses around the block if

(placing them around foreach

or inside if

doesn't change anything) makes everything work like a charm, since there is only one statement below if

and therefore you don't need them there:

class Whatever
{
    ...

    private function requireFile($filePath)
    {
        if(is_array($filePath)) {
            foreach($filePath as $singleFilePath)
                if($this->requireFile($singleFilePath))
                    break;
        } elseif(($filePath = stream_resolve_include_path($filePath = $filePath . '.php')) !== false)
            return require_once $filePath;
    }
}

      

Can anyone help me? What's wrong with this code?

Edit: thanks people. I think your answers prove it was me slow here, ha ha ha. Months of programming make people write crazy things ...

+3


source to share


2 answers


Without parentheses, the PHP parser parses the statement elseif

against the second if

, not the first (outer). Adding parentheses tells the parser which elseif

belongs to the first if

.



PHP is not like Python, where indentation tells the parser the relationship between statements.

+3


source


My guess is that when you put parentheses around the if statement it works. Anyway, why doesn't it work.

           if($this->requireFile($singleFilePath))
                break;
    elseif(($filePath = stream_resolve_include_path($filePath = $filePath . '.php')) !== false)
        return require_once $filePath;

      



elseif

grouped with the last operator if

encountered in this case with the inner one if

, you need to add parentheses around the outer if

one to explicitly tell the parser what the else if

outer, if not the inner , belongs to if

. Indentation has nothing to do with PHP.

On another note, you have an unnecessary assignment in the elseif branch: $filePath = resolve_include_path($filePath = $filePath . '.php')

can be done without a second assignment $filePath = resolve_include_path($filePath . '.php')

.

+1


source







All Articles