如何获取当前的 PHP 页面名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13032930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:41:04  来源:igfitidea点击:

How to get current PHP page name

php

提问by Random Guy

I've a file called demo.phpwhere I don't have any GET variables in the URL, so if I want to hide a button if am on this page I can't use something like this:

我有一个名为的文件demo.php,其中 URL 中没有任何 GET 变量,因此如果我想隐藏此页面上的按钮,则无法使用以下内容:

if($_GET['name'] == 'value') {
  //Hide
} else {
  //show
}

So I want something like

所以我想要类似的东西

$filename = //get file name
if($filename == 'file_name.php') {
  //Hide
} else {
  //show
}

I don't want to declare unnecessary GET variables just for doing this...

我不想为了这样做而声明不必要的 GET 变量......

回答by Mr. Alien

You can use basename()and $_SERVER['PHP_SELF']to get current page file name

您可以使用basename()$_SERVER['PHP_SELF']获取当前页面文件名

echo basename($_SERVER['PHP_SELF']); /* Returns The Current PHP File Name */

回答by Amykate

$_SERVER["PHP_SELF"];will give you the current filename and its path, but basename(__FILE__)should give you the filename that it is called from.

$_SERVER["PHP_SELF"];将为您提供当前文件名及其路径,但basename(__FILE__)应该为您提供调用它的文件名。

So

所以

if(basename(__FILE__) == 'file_name.php') {
  //Hide
} else {
  //show
}

should do it.

应该这样做。

回答by Bogdan Burym

In your case you can use __FILE__variable !
It should help.
It is one of predefined.
Read more about predefined constants in PHP http://php.net/manual/en/language.constants.predefined.php

在您的情况下,您可以使用__FILE__变量!
它应该有帮助。
它是预定义之一。
阅读有关 PHP 中预定义常量的更多信息http://php.net/manual/en/language.constants.predefined.php