[英]How can I echo php with php?
我正在開發的網站使用“標題”和“頁腳”模板。 這些模板包含在網站的每個頁面上。 標題模板打開HTML並包含完整的head標記,然后打開body標記。 頁腳模板關閉body標簽,然后關閉HTML。 基本結構如下:
<?php
//Set the current page name so it can be highlighted in the menu bar
$page="Home";
//For page-specific HEAD includes
$headincludes=array("<some type of code>","<some other type of code>");
include("/assets/template/header.php");
?>
<!--START PAGE CONTENT-->
<!--END PAGE CONTENT-->
<?php
include("/assets/template/footer.php");
?>
標題模板將“headincludes”數組轉儲到頁面的head部分,如下所示:
foreach ($headincludes as $toecho)
echo $toecho;
這種方法適用於諸如javascript之類的東西,但是當我嘗試添加PHP語句時,它最終會在提供給瀏覽器的最終頁面中作為未解析的PHP。 是否有一些特殊的方法用PHP回顯PHP?
是的, eval()
(閱讀evil()
)但是你最好重新考慮你的方法。 也許,如果您希望包含大量的PHP代碼,請傳遞一系列可以進一步包含的文件:
// at the beginning of the file
$includes = array('db.php', 'config.php');
// in your header, or footer (whichever is applicable)
foreach($includes as $file){
if(file_exists($file)){
include $file; // or include_once
}
}
然后,使用動態包含,您可以根據需要管理和加載其他代碼。
從字符串執行PHP的方法是使用eval()
,但它很少使用正確的工具。
你可以把你需要的PHP放在$headincludes
,然后將它的輸出添加到$headincludes
嗎?
<?php
//Set the current page name so it can be highlighted in the menu bar
$page="Home";
include 'generators.php';
$links = generateLinks('some_arg');
//For page-specific HEAD includes
$headincludes=array("<some type of code>","<some other type of code>", $links);
include("/assets/template/header.php");
...
你在為陣列添加什么?
如果你這樣設置你的PHP:
$headincludes=array("$var1","$var2");
不起作用。 你必須這樣做:
$headincludes=array($var1,$var2);
在主文件中執行PHP代碼並將結果轉儲到$headincludes
。 沒有必要在標題中執行代碼,結果不會改變。
或者是嗎? 如果你真的試圖將任意代碼注入到其他代碼中,那么你做錯了。
什么是$headincludes
注意 :使用include_once
你嘗試過串聯連接嗎? 像這樣
$headincludes=array("<your html code>".$variable."<more html>","<more>")
嘗試這個:
echo eval('?>html or javascript<?');
echo eval('php');
如果您嘗試輸出php文件,請確保您輕松完成,但您的瀏覽器將無法運行。
所以你需要:
要么
無論如何, include_once()或include()都是你的朋友,它(就像名字讀取一樣)包含文件,文件在那個地方被“執行”。 如果之前已經包含過,那么“once”版本將不會包含相同的文件兩次(如果您執行多個包含,則會很有用,這反過來又會包含其他內容)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.