Home > Backend Development > PHP Tutorial > 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

黄舟
Release: 2023-03-06 10:48:01
Original
1746 people have browsed it

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/
Copy after login

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
stringRequired. 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呢?因为要排除前面的那个 /
Copy after login

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;
}
Copy after login

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)!



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template