为什么我不能在php的try块中定义函数之前调用它
问题描述:
当我在定义之前在try块内调用函数时.它给了我致命错误
When I call a function inside try block before defining. it gives me fatal error
我想做的是这个
try {
echo someFunction();
function someFunction()
{
return 'hello';
}
} catch (Exception $e ){
return $e->getMessage();
}
尽管我只是在try块上方修复了此粘贴功能,但我很好奇这里出了问题,应该行不通.它不在条件块中.
Although I just fix this just paste function above the try block I am curious what is wrong here should not it work. 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();
}