2015-06-11 24 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;