开发者

A function inside an if structure

can I put a function in PHP inside a if structure? like this:

<?php
    if(true){
         function HelloWorld(){
             echo "Hello World!!!";
         }
         HelloWorld();
    }
?>开发者_Python百科;

because I have tried and it works, but I don't know if it is correct or not. Thanks


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.


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";
    }
}
  • function_exists documentation


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


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.


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! ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜