I recently came across a weird piece of code

if(foo == bar){
 function foo_bar(){
   // Function definition

Is this a good practice of coding ?

1 Answer 1


There is one valid use case:

if( !function_exists("helloWorld") ) {
    function helloWorld() {

Other than that, it would be preferable to avoid defining your functions inside if blocks.

  • 4
    +1, there are enough landmines in PHP without creating your own. Aug 16, 2012 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.