[英]Why shouldn't I use shell_exec rather than output buffering to 'include' a file into a variable?
我有一個模板來處理在每個頁面上重復的所有各種內容。 問題是,所有不同的東西都放在模板的中間。 大多數人都熟悉這一點,但這里有一些偽標簽作為視覺輔助:
<begin_template>
<top_part_of_template_with_repeated_stuff>
<!--different stuff goes here-->
<bottom_part_of_template_with_more_repeated_stuff>
<end_template>
我知道有各種不同的方法來處理這個問題。 我不喜歡這樣做:
include 'top_part_of_template.html';
// output the different part
include 'bottom_part_of_template.html';
因為在單獨的文檔中打開和關閉標簽讓我感到困擾。 我目前正在做的是一個這樣的模板:
<template>
<?php if (isset($different_stuff)) echo $different_stuff ?>
<more_template>
並使用像這樣的輸出緩沖來獲取不同的東西:
ob_start();
include 'different_stuff.php';
$different_stuff = ob_get_clean();
include 'template.php';
我遇到了這個答案,它建議使用shell_exec
。 雖然我大致了解shell_exec
作用,但我對使用它並不十分熟悉。 我是這樣試的:
$different_stuff = shell_exec('php different_stuff.php');
include 'template.php';
它確實有效,我喜歡它只是因為它是一個語句而不是三個語句。 不過,這感覺是個壞主意(主要是因為我認為如果這是個好主意,我會更頻繁地使用它,這是我第一次看到它)但我對shell_exec
不夠熟悉,不知道為什么.
我認為這聽起來像是一個基於意見的問題,但我真的不是在尋找意見。 我想知道不使用這種方法的客觀、可證明的原因。
您正在以這種方式啟動一個新的 shell 環境和一個新的 PHP 實例,而不是利用您的代碼已經在其中運行的 PHP 實例。 所以有性能和內存的影響。
許多服務器都有單獨的 php.ini 文件用於命令行 PHP(您的 shell_exec 調用)和基於 FPM 的 PHP(大多數現代 Web 服務器調用)。
而且,shell_exec 不會傳回任何錯誤信息,因此您將更難檢測問題並對其進行故障排除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.