为什么我不能在php的try块中定义函数之前调用它 [英] Why can't i call a function before defining it in a try block in php

查看:91
本文介绍了为什么我不能在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();
}

http://php.net/manual/en/functions.user -defined.php

这篇关于为什么我不能在php的try块中定义函数之前调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆