relative path in require_once doesn't work

The require_once function in PHP is used to include a specific PHP file in another file, but it will only include the file once to prevent multiple declarations.

When using a relative path with require_once, it is important to understand that the path is relative to the file that is using the require_once statement, not the file that is being executed.

Watch a course Learn object oriented PHP

If you are having trouble getting a relative path to work with require_once, make sure that the path is correct and that the file you are trying to include exists in the specified location. If the file is in a different directory, you may need to use a relative path that goes up one or more directories (e.g. ../) in order to reach the correct location.

In addition, it's always a good practice to use the absolute path instead of relative path, this will help you to avoid the path problem.

Also if you are using a virtual host and using a different base directory make sure that the path is relative to the root directory of the virtual host.

You may also check the permissions of the file and the folder that you're trying to access, to ensure that the PHP process has read access to them.