PHP中的动态标题标签 [英] Dynamic Title Tag in PHP
问题描述
我正在尝试在网站上动态填充标题标签.我的 index.php 页面中有以下代码
I am trying to dynamically populate the title tag on a website. I have the following code in my index.php page
<?php $title = 'myTitle'; include("header.php"); ?>
以及我的标题页上的以下内容
And the following on my header page
<title><?php if (isset($title)) {echo $title;}
else {echo "My Website";} ?></title>
但是无论我做什么,我都无法让这段代码工作.有没有人有什么建议?
But no matter what I do, I cannot get this code to work. Does anyone have any suggestions?
谢谢
推荐答案
此方法有效(已测试 - 创建一个新文件夹,将第一行代码放在名为 index.php 的文件中,将第二行代码放在 header.php 中,运行它,检查标题栏).
This works (tested it - create a new folder, put your first line of code in a file called index.php and the second one in header.php, run it, check the title bar).
您应该仔细检查这两个文件是否在同一个文件夹中,并且您包含了正确的 header.php 来自 正确的 index.php.并确保 $title 没有在代码中的某处设置回 null
.
You should double check if those two files are in the same folder, and that you're including the right header.php from the right index.php. And ensure that $title is not being set back to null
somewhere in your code.
在此处了解有关变量范围的更多信息.
Learn more about Variable Scope here.
可见更改的示例是:
TEST1<?php $title = 'myTitle'; include("header.php"); ?>
<title>TEST2<?php if ...
这篇关于PHP中的动态标题标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!