[英]Why can't i call a function before defining it in a try block in php
When I call a function inside try block before defining. 当我在定义之前在try块内调用函数时。 it gives me fatal error
它给我致命错误
What I was trying to do is this 我想做的是
try {
echo someFunction();
function someFunction()
{
return 'hello';
}
} catch (Exception $e ){
return $e->getMessage();
}
Although I just fix this just paste function above the try block I am curious what is wrong here should not it work. 尽管我只是在try块上方修复了此粘贴功能,但我很好奇这里不应该起作用。 it is not inside in conditional block.
它不在条件块中。
When a function is defined in a conditional manner it's definition must be processed prior to being called. 当以条件方式定义函数时,必须先处理其定义,然后再调用它。 Just switch the echo and function like below.
只需切换回显和功能,如下所示。
try {
function someFunction()
{
return 'hello';
}
echo someFunction();
} catch (Exception $e ){
return $e->getMessage();
}
http://php.net/manual/en/functions.user-defined.php http://php.net/manual/zh/functions.user-defined.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.