【问题标题】:PHP Include With VarietyPHP 包括各种
【发布时间】:2013-06-23 19:41:39
【问题描述】:

我的网站“负责人”有一个php include 声明。

我正在使用以下代码调用head.php...

<?php 
    include '../components/head.php' 
?>

在我的head.php 中,我有以下代码...

<head>
    <link rel="stylesheet" type="css" href="/style.css">
    <title>Dummy Code</title>
</head>

如果我的网页上有$title = "About",我如何通过在我的页面上使用一个变量来更改标题,例如Dummy Code | About 作为标题。

有没有办法做到这一点?

【问题讨论】:

  • 看?简单&lt;title&gt;My site - &lt;?php echo $title;?&gt;&lt;/title&gt; ?
  • 把变量$title放在标签中
  • 必须在包含heaad.php之前设置$title

标签: php html include title


【解决方案1】:

它们都属于全局命名空间,所以你可以这样做:

<?php 
    $title = 'about';
    include '../components/head.php' 
?>

head.php:

<head>
    <link rel="stylesheet" type="css" href="/style.css">
    <title>Dummy Code | <?=$title; ?></title>
</head>

但请确保您理解:这是非常简化的代码,不应用于生产项目。

【讨论】:

  • 你的意思是什么这是非常简化的代码,不应该在生产项目中使用。
  • 我的意思是你应该避免包含带有 html 代码的文件,而是使用一些模板引擎。
  • 我不明白,我对 PHP 和 HTML 很陌生。
  • 好的,说几句话。当 PHP 代码与 HTML 结合使用时,这是一种不好的做法。阅读:stackoverflow.com/questions/436014/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
相关资源
最近更新 更多