Home  >  Article  >  Backend Development  >  PHP code example to get the name of the currently executing php file

PHP code example to get the name of the currently executing php file

黄舟
黄舟Original
2017-03-04 11:24:491658browse

This article mainly introduces the code for PHP to obtain the name of the currently executing PHP file. Friends who need it can refer to

This problem encountered in the navigation judgment when writing the confession wall today. My solution is as follows:

Determine the current php file name to determine which part of the navigation bar is highlighted.
How does php get the current url file name?
This is how I handle it:shock:

Tutorial

First of all, we must get the current page URL. Here we use php$ _SERVER['PHP_SELF']To get the web page address
Assume the url is http://www.php.cn/

$php_Self= $_SERVER['PHP_SELF']; //获取网页地址
//输出结果:http://www.php.cn/

Output Later, we found that we only need index.php, and the previous list of things are useless.
How to do it?
Here we use the substr() function.
substr() is used to return part of the string:

substr syntax
substr(string,start,length)


##length
Parameter Description
string Required. Specifies a part of the string to be returned.
start

Required. Specifies where in the string to begin.

  • Positive number – starts at the specified position in the string

  • Negative number – starts at the specified position from the end of the string

  • 0 – Starts at the first character in the string

Optional. Specifies the length of the returned string. The default is until the end of the string.

  • Positive number – the length returned from the position of the start parameter

  • Negative number – the length returned from the end of the string

#See the second parameter of this function, which specifies where to start in the string. Obviously the last /beginning in the url is what we need.

So we need to use the
strrpos() function to get the position of the last occurrence.

strrpos syntax

strrpos(string,find,start)

Parameters DescriptionstringRequired. Specifies the string to be searched for. findRequired. Specifies the characters to search for. startOptional. Specifies where to start the search.
So the total code is as follows:

$php_Self = substr($_SERVER['PHP_SELF'],strripos($_SERVER['PHP_SELF'],"/")+1);
//为啥要加1呢?因为要排除前面的那个 /

A more recommended function

//获得当前的脚本网址 
function GetCurUrl() 
{
  if(!empty($_SERVER["REQUEST_URI"])) 
  {
    $scriptName = $_SERVER["REQUEST_URI"];
    $nowurl = $scriptName;
  } else 
  {
    $scriptName = $_SERVER["PHP_SELF"];
    if(empty($_SERVER["QUERY_STRING"])) 
    {
      $nowurl = $scriptName;
    } else 
    {
      $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
    }
  }
  return $nowurl;
}

This solves the problem. In many cases, we need the help of some string interception functions to get the results we want.


The above is the content of the code example for PHP to obtain the name of the currently executing php file. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn