What does the dot-slash do to PHP include calls?

前端 未结 7 1487
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 09:05

A. What does this do?

require (\"./file.php\");

B. in comparison to this?

require (\"file.php\");

(

7条回答
  •  盖世英雄少女心
    2020-12-01 09:42

    The Short Answer

    You're right, it's not up one directory. A . refers to the directory you're in, and .. refers to the parent directory.

    Meaning, ./file.php and file.php are functionally equivalent in PHP. Here's the relevent page of documentation: http://us.php.net/manual/en/wrappers.file.php

    The Longer Answer

    However, just because they work the same in this context doesn't mean they're always the same.

    When you're operating in a *nix shell environment, and you type the name of an executable file, the shell will look in the PATH directories, but it won't look in the CWD, or the directory you're currently in.

    So, if you're in a directory that has a file called: myprogram.php (this would be a PHP CLI file) and you just type:

    myprogram.php

    it doesn't matter if your program is executable or not. The shell will look in /bin/, /usr/bin/ etc for your file, but it won't look in ./, or the directory you're in.

    To execute that program without adding your directory to the PATH, you need to type

    ./myprogram

    So really, ./ is more explicit. It means, "the file you're looking for HAS to be right here" and no ./ means, "the file should be somewhere the program looking for files".

提交回复
热议问题