获取包含 PHP 脚本的文件的名称

2022-08-30 19:43:41

以下是我正在尝试执行的操作的示例:

索引.php

<ul><?php include("list.php") ?></ul>

列表.php

<?php
    if (PAGE_NAME is index.php) {
        //Do something
    }
    else {
        //Do something
    }
?>

如何获取包含列表的文件的名称.php脚本(PAGE_NAME)?我已经尝试过,但这给了我.basename(__FILE__)list.php


答案 1

$_SERVER["PHP_SELF"];返回您想要的内容


答案 2

如果您确实需要知道当前文件包含了哪个文件 - 这是解决方案:

$trace = debug_backtrace();

$from_index = false;
if (isset($trace[0])) {
    $file = basename($trace[0]['file']);

    if ($file == 'index.php') {
        $from_index = true;
    }
}

if ($from_index) {
    // Do something
} else {
    // Do something else
}

推荐