PHP:包括内部包含不同的根

So I have:

Page A - /WWW/index.php

Page B - /WWW/folder/index.php

Page C - /WWW/assets/functions.php

I want to be able to use the same line of code to include Page C on Page A and Page B.

Right now for Page A: include "/assets/functions.php";

Page B: include "../assets/functions.php";

The "../" is what bugs me. Depending on how many times the file is nested in folder I have to add a "../"

I tried: include dirname(__FILE__) . "/assets/functions.php";

But it works on Page A and not Page B.

Also in the function.php I want to be able to include other PHP files.

require_once($_SERVER["DOCUMENT_ROOT"]. "/assets/functions.php");

require_once is used to include your file once.

$_SERVER["DOCUMENT_ROOT"] is used to get your root directory.

try it

require rtrim(dirname(__FILE__), '/\\') . DIRECTORY_SEPARATOR . 'my_file.php';