函数正在调用文件特定值?
function is calling file specific values?
我在 single.php 文件中有一个非常简单的函数
<?php function file_nameecho(){
$fnam = basename(__FILE__, '.php');
echo $fnam;
}
?>
在 other.php 文件中调用此函数时。
它仍然显示 single.php 文件的名称?
我认为您将具有此函数的文件包含到另一个脚本中,现在您想知道为什么名称仍然是具有该函数的脚本中的名称,而不是调用该函数的名称。
(所以我认为你做了什么:)
file1.php:
function file_nameecho() {
$fnam = basename(__FILE__, '.php');
echo $fnam;
}
file2.php:
include "file1.php";
file_nameecho();
输出将是:
file1.php
为了让你的函数像你想要的那样工作,使用这个:
function file_nameecho(){
$fnam = basename($_SERVER['PHP_SELF'], '.php');
echo $fnam;
}
编辑:
这应该可以帮助您获取调用函数的文件名:
function file_nameecho(){
$fnam = basename(debug_backtrace()[0]["file"], '.php');
echo $fnam;
}
编辑 2:
因此,根据 OP 的要求,对所有内容进行一些解释:D
- 为什么
__FILE__
return定义函数的名字?
__FILE__
是一个魔法常量,return 是文件名。
以及手册中的引述:
The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
因此,有关魔法常量的更多信息,请参阅:http://php.net/manual/en/language.constants.predefined.php
debug_backtrace()
是做什么的?
debug_backtrace()
生成回溯。所以这就是我在函数中使用它的原因。您可以使用 print_r(debug_backtrace());
查看有关数据回溯的所有信息,这也是我发现在您的情况下它是 [0]["file"]
.
的方式
有关更多信息,请参阅:http://php.net/manual/en/function.debug-backtrace.php
http://php.net/manual/en/language.constants.predefined.php
__FILE__
已解析符号链接的文件的完整路径和文件名。如果在包含内部使用,则返回包含文件的名称。
FILE 变量将始终包含正在访问变量的当前文件的文件名,在您的情况下是 "single.php"
查看 PHP Manual 寻求帮助。
它会显示 "single",
FILE 将是 single.php,
可以看到phpFILEmanual.
不,它会显示包含文件的页面。
要调用 function file_nameecho()
,您必须在 other.php 中使用 include ('single.php')
。
当服务器加载页面时 other.php 不能 运行 __FILE__
一个页面与另一个引用
我在 single.php 文件中有一个非常简单的函数
<?php function file_nameecho(){
$fnam = basename(__FILE__, '.php');
echo $fnam;
}
?>
在 other.php 文件中调用此函数时。
它仍然显示 single.php 文件的名称?
我认为您将具有此函数的文件包含到另一个脚本中,现在您想知道为什么名称仍然是具有该函数的脚本中的名称,而不是调用该函数的名称。
(所以我认为你做了什么:)
file1.php:
function file_nameecho() {
$fnam = basename(__FILE__, '.php');
echo $fnam;
}
file2.php:
include "file1.php";
file_nameecho();
输出将是:
file1.php
为了让你的函数像你想要的那样工作,使用这个:
function file_nameecho(){
$fnam = basename($_SERVER['PHP_SELF'], '.php');
echo $fnam;
}
编辑:
这应该可以帮助您获取调用函数的文件名:
function file_nameecho(){
$fnam = basename(debug_backtrace()[0]["file"], '.php');
echo $fnam;
}
编辑 2:
因此,根据 OP 的要求,对所有内容进行一些解释:D
- 为什么
__FILE__
return定义函数的名字?
__FILE__
是一个魔法常量,return 是文件名。
以及手册中的引述:
The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
因此,有关魔法常量的更多信息,请参阅:http://php.net/manual/en/language.constants.predefined.php
debug_backtrace()
是做什么的?
debug_backtrace()
生成回溯。所以这就是我在函数中使用它的原因。您可以使用 print_r(debug_backtrace());
查看有关数据回溯的所有信息,这也是我发现在您的情况下它是 [0]["file"]
.
有关更多信息,请参阅:http://php.net/manual/en/function.debug-backtrace.php
http://php.net/manual/en/language.constants.predefined.php
__FILE__
已解析符号链接的文件的完整路径和文件名。如果在包含内部使用,则返回包含文件的名称。
FILE 变量将始终包含正在访问变量的当前文件的文件名,在您的情况下是 "single.php"
查看 PHP Manual 寻求帮助。
它会显示 "single", FILE 将是 single.php, 可以看到phpFILEmanual.
不,它会显示包含文件的页面。
要调用 function file_nameecho()
,您必须在 other.php 中使用 include ('single.php')
。
当服务器加载页面时 other.php 不能 运行 __FILE__
一个页面与另一个引用