__dir__ in php
<?php
include __DIR__ . "/worker_2.php";
3.88
7
PHP scripts run relative to the current path (result of getcwd()),
not to the path of their own file.
Using __DIR__ forces the include to happen relative to their own path.
CORRECT: in file2.php: include (__DIR__ . "/file3.php");
To demonstrate:
- file1.php
- dir/
- file2.php
- file3.php
If file2.php includes file3.php like this: include `file3.php`.
It will work fine if you call file2.php directly.
However, if file1.php includes file2.php,
the current directory (getcwd()), will be wrong for file2.php,
so file3.php cannot be included.
Thank you!
7
0
Are there any code examples left?
New code examples in category PHP
-
PHP 2022-03-27 22:40:03 index:of curiosa 2019 mp4
-
PHP 2022-03-27 22:35:13 Dijkstra’s algorithm php
-
PHP 2022-03-27 22:35:12 get_declared_classes
-
PHP 2022-03-27 22:30:23 php move index of a value to first position in array
-
PHP 2022-03-27 22:25:01 wordpress get_date
-
PHP 2022-03-27 21:30:38 php shorten string with dots
-
PHP 2022-03-27 21:10:46 php header 500
-
PHP 2022-03-27 21:05:06 laravel schedule run
-
PHP 2022-03-27 20:25:12 Get page title, excerpt or content by id
-
PHP 2022-03-27 20:00:27 php convert date from dd/mm/yyyy to yyyy-mm-dd