Where should I put ...
 

Subscribe now and choose from over 30 free gifts worth up to £49 - Plus get £25 to spend in our shop

[Closed] Where should I put my PHP functions?

3 Posts
4 Users
0 Reactions
39 Views
 PJay
Posts: 4884
Free Member
Topic starter
 

I'm slowly trying to teach myself PHP and am currently looking at functions. I've very little programming experience but as a once BBC Basic user I'm used to putting funcions and procedures out of the way at the end of the code. PHP allows this but the examples in the book place them at the start of the code; during a very brief flirtation with C++ I learnt that its functions needed to be at the start of the code as calling a function before it had been defined caused an error.

Since I'm starting out afresh, as it were, would it be considered good practive in PHP (and general programming) terms to put my functions at the start of the code therefore defining them before they're called?


 
Posted : 27/02/2010 10:02 am
Posts: 0
Free Member
 

I'd say it's standard practice to put functions at the top in most languages.
Others may disagree, but they're wrong 🙂


 
Posted : 27/02/2010 10:09 am
Posts: 8177
Free Member
 

Can you specify them in a separate file and use includes? That way you only need to define them once, and you can use them in any subsequent pages by just including the function file? Or does PHP not allow that


 
Posted : 27/02/2010 11:35 am
Posts: 7982
Free Member
 

What woody2000 says. Make sure you understand the difference between include() and require() though.


 
Posted : 27/02/2010 12:06 pm