if 结构内的函数
我可以将 PHP 中的函数放入 if 结构中吗?像这样:
<?php
if(true){
function HelloWorld(){
echo "Hello World!!!";
}
HelloWorld();
}
?>
因为我已经尝试过并且它有效,但我不知道它是否正确。谢谢
can I put a function in PHP inside a if structure? like this:
<?php
if(true){
function HelloWorld(){
echo "Hello World!!!";
}
HelloWorld();
}
?>
because I have tried and it works, but I don't know if it is correct or not. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是完全合法的 - 它只是在 if 语句块中定义了函数。也就是说,为什么你想要这样做有点神秘。
还值得注意的是,该函数将在全局范围内可用(即:在 if 语句块之外),因为...
有关详细信息,请参阅 PHP 用户定义函数手册页。
This is perfectly legal - it simply defines the function within the if statement block. That said, quite why you'd want to do this is somewhat of a mystery.
It's also worth noting that this function will be available in the global scope (i.e.: outside of the if statement block), as...
See the PHP User-defined functions manual page for more information.
正如 middaparka 所说,这是完全合法的,但是在使用它方面,您可能需要在声明函数之前检查函数是否存在:
function_exists
文档As middaparka says, this is perfectly legal, but in terms of using it, you might want to check if a function exists before declaring it:
function_exists
documentation它看起来有点奇怪,但是是的,它是合法的
从 PHP 5.3 开始,如果您需要一个范围有限的函数(例如回调),您可以使用 匿名函数/闭包
It looks a bit strange, but yes it's legal
As of PHP 5.3 if you need a function with a limited scope (say for a callback) you can use anonymous functions / closures
我想在这里添加一个答案,因为其他答案中没有解决一个警告。
根据我在 5.3 中的测试,在
if
结构中定义的函数似乎是在运行时定义的,而不是像其他函数一样在编译时定义。例如,在条件块之外, this:
工作正常,因为该函数是在编译时定义并在运行时执行的。但是在条件块内定义函数之前调用该函数:
将产生
调用未定义函数
错误,至少从版本 5.3 开始。但是在调用函数之前在 if 块中定义函数是有效的:
因此,在条件内部定义函数时需要注意一个主要问题:函数必须在调用函数之前在代码中定义,因为条件函数不会似乎是在编译时定义的。
OP 还询问了性能。如果条件函数是在运行时定义的,而不是像大多数其他函数一样在编译时定义的,那么该函数将无法受益于 OpCode 缓存等性能增强器,根据情况,这可能会减慢应用程序的速度。例如,如果您的主 php 文件如下所示:
那么整个应用程序可能根本无法从 OpCode 缓存中受益。
I wanted to add an answer here, because there's a caveat that isn't addressed in the other answers.
Based on my testing in 5.3, it appears that functions that are defined inside
if
structures are defined at runtime, rather than at compile time like other functions.For example, outside a conditional block, this:
works fine because the function is defined at compile time, and executed at runtime. But calling the function before defining it while inside a condition block:
Will produce a
call to undefined function
error, at least as of version 5.3.But defining the function in an if block before calling it is valid:
So there is a major gotcha to be aware of when defining functions inside a conditional: the function must be defined in the code before calling the function, since conditional functions don't seem to get defined at compile time.
The OP also asked about performance. If the conditional function is defined at runtime instead of compile time like most other functions, then this function will not benefit from performance boosters like OpCode caching, which depending on the circumstances, could slow down your application. For example, if your master php file looked like:
Then the entire application might not benefit from OpCode caching at all.
基本上是的,根据手册:
就我个人而言,我还没有遇到过真正会做这样的事情的情况。大多数时候,将函数分组到实际可以找到的位置会更容易! ;-)
Basically yes, according to the manual:
Personally I haven't had a situation in which I'd actually do such a thing. Most of the time it would be easier to group your functions on a place where it actually can be found! ;-)