PHP include relative path
In PHP, you can use the include
or require
statement to include a file with a relative path. The relative path is relative to the file that contains the include
or require
statement. For example, if you have a file called "header.php" in a directory called "includes" and you want to include it in a file called "index.php" that is in the same directory as the "includes" directory, you would use the following code in the "index.php" file:
<?php include 'includes/header.php'; ?>
Alternatively, you can use the __DIR__
magic constant to make the path relative to the current file.
<?php include __DIR__ . '/includes/header.php'; ?>
It is important to note that if the file specified in an include statement does not exist or cannot be read, a warning message will be generated and the script will continue to execute. To avoid this, use require
instead of include
which will cause the script to stop execution when the file is not found.