How to get current PHP page name

Php

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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 */

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

Solution 3 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRandom GuyView Question on Stackoverflow
Solution 1 - PhpMr. AlienView Answer on Stackoverflow
Solution 2 - PhpAmykateView Answer on Stackoverflow
Solution 3 - PhpBogdan BurymView Answer on Stackoverflow