if结构中的函数 [英] A function inside an if structure
问题描述
我可以在一个if结构中使用PHP函数吗?像这样:
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
推荐答案
这完全合法 - 它只是在if语句块中定义函数。这就是说,为什么你想这样做有点神秘。
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.
值得注意的是,这个函数可用在全局范围内(即:在if语句块之外),作为...
It's also worth noting that this function will be available in the global scope (i.e.: outside of the if statement block), as...
PHP中的所有函数和类都有
是全局作用域 - 即使它们是内部定义的
,也可以在函数外部调用
,反之亦然。
All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
请参阅PHP 用户定义的功能手册页面了解更多信息。
See the PHP User-defined functions manual page for more information.
这篇关于if结构中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!