Welcome Guest, Not a member yet? Register   Sign In
Alternative PHP Syntax question
#1

[eluser]Chris Newton[/eluser]
Code:
private function _mkdir_recursive($pathname, $mode=0777)
    {
        is_dir(dirname($pathname)) || $this->_mkdir_recursive(dirname($pathname), $mode);
        return is_dir($pathname)  || mkdir($pathname, $mode,TRUE);
    }

Recursive functions always have my brain in a twist, and this one uses some obscure form of PHP syntax that I'm just not getting. Can anyone please take a moment and write this out in long PHP, or at least direct me to the PHP documentation that explains how this syntax works. Every time I try to make sense of it using standard if/else statements it just doesn't seem to get to the point that it works or makes sense to me.

I know the code WILL work to create folders, but I'd prefer to understand HOW. I just keep staring at it, and the code doesn't make any sense to me. Any help would be greatly appreciated.
#2

[eluser]Randy Casburn[/eluser]
Here you go.

Code:
function _mkdir_recursive($pathname, $mode=0777)
{
    $myDir = $pathname;
    
    $test = is_dir($myDir);

    if($test !== true)
    {
        mkdir($pathname, $mode, TRUE );
        
        $up_one_level = dirname($pathname)); //dirname() returns one level higher

        _mkdir_recursive($up_one_level, $mode);  
    }
    
    return is_dir($pathname);
}

Randy
#3

[eluser]Randy Casburn[/eluser]
So then...in pseudo code:
Code:
if
    the path ending is not a directory  
       OR  
         the next higher level path ending is not a directory
         OR  
           the next higher level path ending is not a directory
           OR  
             the next higher level path ending is not a directory
             OR  
               the next higher level path ending is not a directory
                                 |
                                 |
                                 |
                                 |
                                 |
  then when you finally find a directory return the directory name
     OR
      what you've found is not a directory so it must be created as a directory

Does this make sense?

[edit] I realized this sequence is off a little, but it get's the point across. If you look at the 'long PHP' as you call it, you'll see the directories are created during the recursions and not at the end as I imply in this psuedo code [/edit]

Randy
#4

[eluser]Chris Newton[/eluser]
Got it. My confusion came from me reading the dirname documentation wrong. From the docs it looked like dirname(my/url/path) would return "my" rather than "my/url"

Now it makes sense.
#5

[eluser]Chris Newton[/eluser]
Oh, and Thanks a million!
#6

[eluser]matthewr[/eluser]
I don't get the original code either. I get it when it's translated.
#7

[eluser]Randy Casburn[/eluser]
[quote author="mahuti" date="1217393531"]Got it. My confusion came from me reading the dirname documentation wrong. From the docs it looked like dirname(my/url/path) would return "my" rather than "my/url"

Now it makes sense.[/quote]

I've edited to the translation to make the dirname stand out a bit more.

Randy
#8

[eluser]nmweb[/eluser]
The syntax is also not the fastest I belief
Code:
(true) AND something();
Is slower than
Code:
if(true)
{
something();
}
#9

[eluser]Randy Casburn[/eluser]
Huh?
#10

[eluser]Chris Newton[/eluser]
This is what I wrote up that works identically to the original code:

Code:
private function _mkdir_recursive($pathname, $mode=0777)
    {
        if (is_dir(dirname($pathname)) !== TRUE)
        {
            $this->_mkdir_recursive(dirname($pathname), $mode);
        }
        if (is_dir($pathname) !== TRUE)
        {
            return mkdir($pathname, $mode,TRUE);
        }
    }

Which, by the way, is pretty handy code to create a bunch of nested folders, even if one of the folders up the chain has already been created it continues making the rest.




Theme © iAndrew 2016 - Forum software by © MyBB