1

I wanted to include some files from a directory , so my code had :

foreach (glob("inc/*.php") as $filename)
{
    include $filename;
}

This syntax is referenced almost in all related items on the net ( example question on SE )

But .. there were no results.

So I tried

 foreach (glob(".inc/*.php") as $filename)
    {
        include $filename ;
    }

and then :

foreach (glob("/inc/*.php") as $filename)
    {
        include $filename;
    }

And finally I added dirname(__FILE__) :

 foreach ( glob( dirname(__FILE__) . '/inc/*.php' ) as $filename) {
    include( $filename );
}

Which worked .

So everything is ok now ( no need to panic ) , but for the sake of understanding , I have some issues I want to clarify :

1 . Why did glob() worked for me only when I added dirname(__FILE__) ?

2 . Is there a difference in syntax between *NIX and windows servers ?

3 . It is actually the first time I noticed the syntax include $filename - where are the parenthesis ? why they are not needed in this context?

Community
  • 1
  • 1
Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • 1
    1. Wrong current directory, 2. not in this case, 3. because it's a language construct, not a function. – mario Dec 01 '13 at 16:18
  • Thanks , But why was it wrong current directory ? The file for my understanding was in the right place ( same dir as the `inc` subdir.. ) – Obmerk Kronen Dec 01 '13 at 16:21
  • 1
    Paths are not relative to the current script, but to the current working directory. – mario Dec 01 '13 at 16:23
  • @mario ok :-) that would explain a lot :-) Are you sure you do not want to add as answer ? – Obmerk Kronen Dec 01 '13 at 16:25

0 Answers0