PHP 回显不起作用 <title> - 为什么?

  • 本文关键字:title 不起作用 PHP php
  • 更新时间 :
  • 英文 :


我有以下文件结构:

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'; 
?>

最新更新