How to get current PHP page name

I've a file called demo.php where 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:

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...

357597 次浏览

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

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

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

$_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.

So

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

should do it.