Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently came across a weird piece of code

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

Is this a good practice of coding ?

share|improve this question
up vote 8 down vote accepted

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.

share|improve this answer
4  
+1, there are enough landmines in PHP without creating your own. – Wyatt Barnett Aug 16 '12 at 10:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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