简体   繁体   English

为什么在PHP的try块中定义函数之前不能调用函数

[英]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.

相关问题 为什么我不能在 PHP 中调用这个受保护的函数? - Why can't i call this protected function in PHP? 如何在 PHP 的不同块中调用函数? - How can I call the function in diferent block in PHP? 不明白为什么我不能调用一个函数 - Not understanding why i can't call a function 在PHP中定义之前使用函数 - Using a function before defining it in PHP 如何在函数调用前注入和删除php代码 - How can I inject and remove php code before a function call 如何正确处理 Guzzle ClientException? 我可以把我的调用放到像 PHP 中的 Java try catch 块中吗? - How can I correctly handle a Guzzle ClientException? Can I put my call into something like a Java try catch block in PHP? 为什么我不能通过 PHP 在准备好的查询中调用 MySQL 存储函数? - Why can't I call a MySQL stored function in a prepared query via PHP? 在PHP中,为什么可以在定义之前调用一个函数,但是在定义之前不能使用变量? - In PHP, why can a function be called before definition, but a variable can't be used before definition? 我可以在大量HTML上使用PHP try / catch吗? - Can I use PHP try/catch around a large block of HTML? 我无法在try catch块中捕获Facebookexceptions - I can't catch Facebookexceptions in try catch block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM