【发布时间】:2011-12-15 07:23:54
【问题描述】:
我想知道我的 PHP 代码中复杂的 if/else 结构是否是一个糟糕的设计决策。是否有大量 if 语句会使 PHP 运行缓慢、网站加载速度变慢等?
这是代码: (我不知道 wordpress 如何处理 is_page 等)
<?php
if (is_page()) {
//
if (is_page(122)) {
//subscribe page
echo "subscribe page";
}
elseif (is_page(1263)) {
//photography course
echo "photography course page";
}
elseif (is_page(array(210,184,128))) {
//210 copyright policy, 184 privacy policy, 128 contact
echo "this is either copyright policy, privacy or contact page!";
//nothing happens here, we don't need social buttons on these pages.
}
elseif (is_page(array(379,71,7,45,124,8,105,175,9,125,110))) {
//379 photo galleries, 71 car photos, 7 conceptual, 45 event photos, 124 fashion, 8 landscape, 105 misc, 175 journalism, 9 portrait, 125 street photography, 110 travel
echo "gallery pages and albums";
}
else {
//any other page
echo "any other page";
}
//
}
elseif (is_single()) {
//
if (in_category(array(147,196,35))) {
//147 car photography, 196 car wallpapers, 35 photo stories
echo "photo posts";
}
else {
//any other post
echo "any other post";
}
//
}
elseif (is_archive()) {
//
//any category
echo "this is archive template"
}
//
?>
【问题讨论】:
-
什么是“分配”? 10? 100? 10千亿?你在做一长串独立的ifs吗?还是 if/then/else/else/else 链?
-
您好,感谢您的回复,我在主帖中添加了代码(因为格式正确)。请检查并告诉我您的想法! :) 谢谢。
标签: php performance