函数头与包含头 php? - 优点和缺点
Function Header vs Include Header php? - Upsides and Downsides
我主要在寻找性能、网站缓存、易于打字和安全性 - 优点和缺点
拆分文件并将它们作为包含项放入,这似乎是一个共同的偏好
<?php
include('header.php');//and whatever includes inside
include('footer.php');//and whatever includes inside
?>
VS
将它们制作成函数并在一个文件中调用它们,您可以在其中检查值并在两个函数之间共享它们,例如多样式选择
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/more-functions.php';
if(isset($_cookie['style']) || isset($_session['style'])){
if(isset($_cookie['style'])){$style = $_cookie['style']}else{$style = $_session['style']}
}
function siteheader($style){
if(!isset($style)){$style = 'default';}
print ('<http>');//insert rest of header
}
function sitefooter($style){
if(!isset($style)){$style = 'default';}
print ('</http>');//insert rest of footer
}
?>
include('header.php');//and whatever includes inside
include('footer.php');//and whatever includes inside
这打破了 PSR-1。
虽然使用页眉和页脚几乎不可避免,无论是显式调用还是隐式调用,都会导致格式错误的 HTML 片段,但当您显式调用它时(作为 function/method),您可以至少在同一个文件中定义了两个片段。
性能不会有显着差异。
我主要在寻找性能、网站缓存、易于打字和安全性 - 优点和缺点
拆分文件并将它们作为包含项放入,这似乎是一个共同的偏好
<?php
include('header.php');//and whatever includes inside
include('footer.php');//and whatever includes inside
?>
VS
将它们制作成函数并在一个文件中调用它们,您可以在其中检查值并在两个函数之间共享它们,例如多样式选择
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/more-functions.php';
if(isset($_cookie['style']) || isset($_session['style'])){
if(isset($_cookie['style'])){$style = $_cookie['style']}else{$style = $_session['style']}
}
function siteheader($style){
if(!isset($style)){$style = 'default';}
print ('<http>');//insert rest of header
}
function sitefooter($style){
if(!isset($style)){$style = 'default';}
print ('</http>');//insert rest of footer
}
?>
include('header.php');//and whatever includes inside
include('footer.php');//and whatever includes inside
这打破了 PSR-1。
虽然使用页眉和页脚几乎不可避免,无论是显式调用还是隐式调用,都会导致格式错误的 HTML 片段,但当您显式调用它时(作为 function/method),您可以至少在同一个文件中定义了两个片段。
性能不会有显着差异。