2015-06-11 25 views
1

我正在尝试使用PHP创建网站。对于我的头文件代码,我在includes/header.php文件中包含所有头文件代码。因此,每个页面开始与如何从PHP标签以外设置变量

<?php 
include("includes/header.php"); 
?> 

这工作,但我需要一些定制,所以如果我需要JS什么的仅添加到第1页。这就是我的目标为:

<?php 
    $header_code = 
    ?> 
     Hi there, this is some code that can be used with a echo $header_code in the includes/header.php file. 
    <?php 
    ; //end of the header_code variable 
?> 
<?php 
    include("includes/header.php"); 
?> 

比在header.php文件有回音$header_code,而不是:

<?php $header_code = "This is some header code..."; ?> 

因为可能有大量的代码在这里。

感谢提前:)

回答

1

不要急于用代码混淆。

您可以在页面顶部初始化变量像

<?php 
    $header_code =<<<EOT 
      <script src='jquery.js'> </script> 
      <script src='jquery2.js'> </script> 
     EOT;; 

     include("includes/header.php"); 

?> 

而且里面的header.php

if(!empty($header_code)) 
{ 
    echo $header_code; 
} 
1

你可以尝试HEREDOC语法。

像这样的事情

$header_code = <<<EOT 
    Hi there, this is some code that can be used with a echo $header_code in the includes/header.php file. 
EOT;