【问题标题】:Using php include function in html within php variable在 php 变量中的 html 中使用 php include 函数
【发布时间】:2014-12-20 04:25:28
【问题描述】:

我正在做这样的事情

   $some_var='<!DOCTYPE html>
              <head>
              <title>questions '.$suto.'</title>
              <meta charset="utf-8" />
              <meta name="description" content="question need to'. $suto.'">
              '. include ("header.php") .'
              <body>       

下面还有更多代码可以运行,所以不在这里发布。

一切都有效,而不是包含功能。

【问题讨论】:

    标签: php html include


    【解决方案1】:

    您可以使用文件获取内容功能 (http://php.net/manual/en/function.file-get-contents.php)

    $some_var='<!DOCTYPE html>
               <head>
               <title>questions '.$suto.'</title>
               <meta charset="utf-8" />
               <meta name="description" content="question need to'. $suto.'">
               '. file-get-contents ("header.php") .'
    
               <body>';     
    

    【讨论】:

      【解决方案2】:

      你可以用file_get_contents()代替include()

      $some_var='<!DOCTYPE html>
          <head>
                  <title>questions '.$suto.'</title>
                   <meta charset="utf-8" />
                   <meta name="description" content="question need to'.$suto.'">'.file_get_contents("header.php").'
          <body>   
      

      file_get_contents() 是一个 PHP 函数,它是将文件内容读入字符串的首选方法之一。

      include() 将解析包含文件中的 PHP 代码。而file_get_contents() 将返回文件的内容。在这里,您将内容存储到字符串中。所以file_get_contents 是正确的选择。

      【讨论】:

      • ty jenz 它可以工作,但为什么在这种情况下包含函数失败
      • @sukhjitdhot..抱歉耽搁了。请查看我更新的答案以获取更多详细信息。
      • thanks jenz file-get-contents ("../header.php") is not working 你能建议如何从以前的目录中选择标题
      • 如果 header.php 位于下面的文件夹中,那么 ("../header.php") 应该可以工作。
      • 这正是我所希望的,但由于某种原因它不起作用。
      猜你喜欢
      • 2013-03-11
      • 2011-12-16
      • 2017-09-09
      • 1970-01-01
      • 2023-04-09
      • 2016-09-25
      • 2014-06-04
      • 2020-07-14
      • 2014-11-26
      相关资源
      最近更新 更多