A good PHP code can correctly output results whether it is placed on Windows or Linux or different versions of PHP. This is a good code.
What is easy to say is not easy to do. Most of the time when writing code, it is function-oriented. In the current environment, if you have to rush to see the results immediately, it is basically as convenient as possible.
But in order to write good code and reduce debugging time later, you must consider whether every code you write can adapt to the difficulties you can think of. Solve one at a time, and over time, your code will be able to scale freely.
The relative path is to the folder where the current code file is located.
Absolute paths are relative to the root folder.
When the code needs to depend on other files, the include path of the code needs to be unified.
When the code is executed, the file cannot be found. Most of the time it is because the path is not defined.
I recommend that you write programs with absolute paths. Once the relative path is moved, it is easy to find that the file to be included cannot be found.
PHP functions and constants used
dirname
__FILE__
DIRECTORY_SEPARATOR
It is recommended to write an initialization file initialize.php
// Define the core paths // Define them as absolute paths to make sure that require_once works as expected // DIRECTORY_SEPARATOR is a PHP pre-defined constant // ( for Windows, / for Unix) defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR); defined('SITE_ROOT') ? null : define('SITE_ROOT', dirname(__FILE__)); defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes'); // load config file first require_once(LIB_PATH.DS.'config.php'); // load basic functions next so that everything after can use them require_once(LIB_PATH.DS.'functions.php'); // load core objects require_once(LIB_PATH.DS.'session.php'); require_once(LIB_PATH.DS.'database.php'); // load database-related classes require_once(LIB_PATH.DS.'user.php'); |
// DIRECTORY_SEPARATOR is a PHP pre-defined constant
// load core objects require_once(LIB_PATH.DS.'session.php'); require_once(LIB_PATH.DS.'database.php');