Disadvantages of this way of writing: It first looks for the specified php include path and then the current directory. So too many paths are checked. If the script is included by a script in another directory, its base directory becomes the directory where the other script is located. Another problem is: when the scheduled task runs the script, its parent directory may not be the working directory. So the best option is to use an absolute path, for example:
The above code defines an absolute path, and the value is hard-coded. Next, with improvements, the path /var/www/project may also change, so do you need to change it every time? No, you can use the __FILE__ constant, for example:
Now , no matter which directory it is moved to, such as moving it to an external server, the code will run correctly without any changes. That is to say, pathinfo and __FILE__ constants are used to achieve portable code. |