php - 使用 basename(__FILE__) 确定页面名称以更改标题不起作用

标签 php html variables if-statement

这很简单,但我想我在这里遗漏了一些关键的东西:

<?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/

相关文章:

html - 选中时如何在复选框周围应用边框?

python - 在 python 中使用 lxml 和 xpath 获取空列表

python - 无法从 cron 作业中找到 dotenv 环境变量

javascript - 定义的 Var 不回显值 jquery 和 php

matlab - 如何在 MATLAB 的循环中创建/处理变量?

php - 帮助 AJAX PHP 和 MYSQL 实时搜索!

php - 语法错误,意外的 T_ECHO,在第 110 行的/Secured_Page_Edit.php 中需要 ',' 或 ';'

javascript - Jquery 生成的输入值未发布在表单提交上

php - 如何为 2 个用户代理加载自定义样式表?

javascript - 通过相同的函数运行不同的元素集