Getting current Page Name in PHP

If you want to get the current page name , php provides you with an inbuilt function $_SERVER[‘PHP_SELF’].
$_SERVER[‘PHP_SELF’] can be used to obtain the current name in the php.It will be necessary for us to use this $_SERVER[‘PHP_SELF’] for action attribute of form element if you want to post the form into the same page. If you use the filename instead of this variable then we may face difficulty when we change the file name.
If the page is located in sub-folder, then this variable will return the page name along with sub-folder name also (e.g learnwebdevelopment/index.php)

If you want to get only the filename (e.g index.php) then you should use basename() as below.

Posted in Php Tagged with: ,