问题描述
是否可以在控制结构中使用包含条件语句的include语句?很难解释,所以这里有一个例子:
Is it possible to use an include statement that contains conditional statements in a control structure? It's hard to explain, so here's an example:
if (...) {
}
elseif (...) {
}
elseif (...) {
}
elseif (...) {
}
else {
}
让我们说我的第三个elseif包含很多代码,我想放在另一个文件中,而是使用include语句。我可以那样做吗?当我尝试这样做时,它给我一个意外的T_ELSE错误。因此可能是这样的:
Let's say my third elseif contained a lot of code and I wanted to put it in another file and use an include statement instead. Could I do that? When I tried to, it was giving my an "unexpected T_ELSE" error. So it would be something like this:
if (...) {
}
elseif (...) {
}
elseif (...) {
}
include 'abc.php';
else {
}
此外,我在在网络上提供了include的法律用法细目分类(我也检出了php.net),例如是否可以将条件语句的条件放在单独的文件中并使用include语句来调用它,或者使用在回显语句中包含语句。如果有人有很好的参考,我将不胜感激。谢谢大家。
Also, I couldn't find many sources on the web that give a breakdown of the legal usage of include (I checked out php.net, too), like if you could put the conditions of a conditional statement in an separate file and use an include statement to call it, or use an include statement in an echo statement. If anyone has some good references, I'd appreciate it. Thank you all in advance.
推荐答案
是的,你可以做到。
正如Jared所指出的那样,问题在于 include
行不在方括号内。
As Jared pointed out, the problem is that the include
line isn't within a bracketed block.
但是,通常不赞成以这种方式有条件地加载代码。您仅应将其用于确定需要加载的内容。我建议研究自动加载所需的内容,作为解决此问题的标准解决方案:
However, conditional loading of code in this manner is typically frowned upon. You should only use it for determining what you will load as you need it. I recommend looking into auto-loading what you need, as the standard solution to this problem:
这篇关于在条件语句中使用PHP include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!