if 结构内的函数

发布于 2024-11-05 05:25:40 字数 234 浏览 3 评论 0原文

我可以将 PHP 中的函数放入 if 结构中吗?像这样:

<?php
    if(true){
         function HelloWorld(){
             echo "Hello World!!!";
         }
         HelloWorld();
    }
?>

因为我已经尝试过并且它有效,但我不知道它是否正确。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

↙温凉少女 2024-11-12 05:25:40

这是完全合法的 - 它只是在 if 语句块中定义了函数。也就是说,为什么你想要这样做有点神秘。

还值得注意的是,该函数将在全局范围内可用(即:在 if 语句块之外),因为...

PHP 中的所有函数和类都有
全局范围 - 它们可以被称为
在函数之外,即使它们是
在内部定义,反之亦然。

有关详细信息,请参阅 PHP 用户定义函数手册页

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.

It's also worth noting that this function will be available in the global scope (i.e.: outside of the if statement block), as...

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.

See the PHP User-defined functions manual page for more information.

亽野灬性zι浪 2024-11-12 05:25:40

正如 middaparka 所说,这是完全合法的,但是在使用它方面,您可能需要在声明函数之前检查函数是否存在:

if (!function_exists("foo"))
{
    function foo()
    {
        return "bar";
    }
}

As middaparka says, this is perfectly legal, but in terms of using it, you might want to check if a function exists before declaring it:

if (!function_exists("foo"))
{
    function foo()
    {
        return "bar";
    }
}
白馒头 2024-11-12 05:25:40

它看起来有点奇怪,但是是的,它是合法的

从 PHP 5.3 开始,如果您需要一个范围有限的函数(例如回调),您可以使用 匿名函数/闭包

It looks a bit strange, but yes it's legal

As of PHP 5.3 if you need a function with a limited scope (say for a callback) you can use anonymous functions / closures

手心的温暖 2024-11-12 05:25:40

我想在这里添加一个答案,因为其他答案中没有解决一个警告。

根据我在 5.3 中的测试,在 if 结构中定义的函数似乎是在运行时定义的,而不是像其他函数一样在编译时定义。

例如,在条件块之外, this:

foo();
function foo(){
   echo 'foo!';
}

工作正常,因为该函数是在编译时定义并在运行时执行的。但是在条件块内定义函数之前调用该函数:

if(1){
   foo(); // Call to undefined function!
   function foo(){
      echo 'foo!';
   }
}

将产生调用未定义函数错误,至少从版本 5.3 开始。

但是在调用函数之前在 if 块中定义函数是有效的:

if(1){       
   function foo(){
      echo 'foo!';
   }
   foo(); // No errors, since function is defined!
}

因此,在条件内部定义函数时需要注意一个主要问题:函数必须在调用函数之前在代码中定义,因为条件函数不会似乎是在编译时定义的。

OP 还询问了性能。如果条件函数是在运行时定义的,而不是像大多数其他函数一样在编译时定义的,那么该函数将无法受益于 OpCode 缓存等性能增强器,根据情况,这可能会减慢应用程序的速度。例如,如果您的主 php 文件如下所示:

if($IsLoggedIn){
   // include files/functions and bootstrap here...
}

那么整个应用程序可能根本无法从 OpCode 缓存中受益。

I wanted to add an answer here, because there's a caveat that isn't addressed in the other answers.

Based on my testing in 5.3, it appears that functions that are defined inside if structures are defined at runtime, rather than at compile time like other functions.

For example, outside a conditional block, this:

foo();
function foo(){
   echo 'foo!';
}

works fine because the function is defined at compile time, and executed at runtime. But calling the function before defining it while inside a condition block:

if(1){
   foo(); // Call to undefined function!
   function foo(){
      echo 'foo!';
   }
}

Will produce a call to undefined function error, at least as of version 5.3.

But defining the function in an if block before calling it is valid:

if(1){       
   function foo(){
      echo 'foo!';
   }
   foo(); // No errors, since function is defined!
}

So there is a major gotcha to be aware of when defining functions inside a conditional: the function must be defined in the code before calling the function, since conditional functions don't seem to get defined at compile time.

The OP also asked about performance. If the conditional function is defined at runtime instead of compile time like most other functions, then this function will not benefit from performance boosters like OpCode caching, which depending on the circumstances, could slow down your application. For example, if your master php file looked like:

if($IsLoggedIn){
   // include files/functions and bootstrap here...
}

Then the entire application might not benefit from OpCode caching at all.

安稳善良 2024-11-12 05:25:40

基本上是的,根据手册

任何有效的 PHP 代码都可能出现在
功能,甚至其他功能和
类定义。

就我个人而言,我还没有遇到过真正会做这样的事情的情况。大多数时候,将函数分组到实际可以找到的位置会更容易! ;-)

Basically yes, according to the manual:

Any valid PHP code may appear inside a
function, even other functions and
class definitions.

Personally I haven't had a situation in which I'd actually do such a thing. Most of the time it would be easier to group your functions on a place where it actually can be found! ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文