我有以下文件结构:
header.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
<!-- Rest of my header -->
anypage.php
<?php include 'header.php'; ?>
<?php $title= "My gorgeous page title"; ?>
<!-- Page content here -->
我用它来添加页面的动态标题,以及动态元标签,出于SEO的原因
问题是:
<?php echo $title ?>
仅在放置在<?php $title= "My gorgeous page title"; ?>
之后时有效。那该怎么办?
因为目前正在包含header.php
,所以$title
还不存在。
那该怎么办?
设置标题后包括header.php
。你的页面越先进,你可能想研究一下提供MVC实现的框架。
您应该在声明$title之后包含header
anypage.php
<?php
$title = "Your page title";
include 'header.php';
?>