这很简单,但我想我在这里遗漏了一些关键的东西:
<?php
$title = " | Unlimited Motors";
if (basename(__FILE__) == "index.php") {
global $title;
$title = "Home"." | Unlimited Motors";
}
if (basename(__FILE__) == "contact.php") {
global $title;
$title = "Contact Us"." | Unlimited Motors";
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
你会认为这么简单的事情会奏效。
las,事实并非如此。 echo $title;
<title></title>
内的部分不获取更改的变量。它简单地显示“| Unlimited Motors”,就像没有 if ()
一样。改变它。
我对这些全局变量和东西有点困惑,所以谁能解释这里的错误?
更新:看来我忘了添加一些我认为不重要的东西:
1) 是的,我已经测试过basename(__FILE__)
在主体中得到“index.php”。
2) 是的,我在头文件中使用它,然后是 included
在其他文件中。
最佳答案
我认为您是在 include
中使用此代码。根据documentation , __FILE__
返回
The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
关于php - 使用 basename(__FILE__) 确定页面名称以更改标题不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31635715/