Portal Home > Knowledgebase > Articles Database > Simple question about php functions


Simple question about php functions




Posted by lexington, 07-07-2009, 08:55 PM
I was wondering if you put all functions in one file and include this file, do the functions act like "switches" in that they are not called at all unless the function is added to the page? I usually keep my function files small, but was wondering if I added more functions to a page does it take memory to store them all, or is that only if they are called on the page itself otherwise it is skipped over like a switch / if statement. Thanks.

Posted by Doh004, 07-07-2009, 09:53 PM
If I understood your question correctly, then yes they will be skipped over like a switch or an if statement. The functions will not run unless they're called.

Posted by lexington, 07-07-2009, 09:58 PM
Great, I wanted to make sure that they were not stored in memory or calculated the stuff within them but just didn't return the data when not called.

Posted by Doh004, 07-08-2009, 07:36 AM
They definitely don't do that, so you're good to go

Posted by jstanden, 07-08-2009, 01:55 PM
Any structures or code you define in the same file will be compiled and loaded into memory when your script runs. The people above seem a little confused. Your functions in the same file won't run unless you call them (like a switch or an include), but they will definitely occupy memory and need to be parsed on compilation/run. Parsing is not the same as executing; the logic you defined won't be run in every function until needed, but the entire script will be translated to opcodes. The amount of processing power and memory used are trivial by today's standards for most things you'll be doing. If you're really worried about it you can use a PHP opcode cache like APC to make sure your scripts only parse when they've changed.

Posted by AdamKDean, 07-08-2009, 02:10 PM
As jstanden said, the code from the include file will still be parsed but it wont "run" unless you call the functions. Remember there are CMS's which use hundreds of different files and functions, all of these are included and parsed at runtime and the pages still process in around 0.1 seconds max.

Posted by Doh004, 07-08-2009, 05:38 PM
I was confused by his terminology and missed the part about going into memory. I think he was more worried about having a function that has an intensive loop being called every time the script is loaded, even when it's not called.

Posted by lexington, 07-13-2009, 02:28 AM
Oops didn't mean to post here Last edited by lexington; 07-13-2009 at 02:35 AM.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Enom down. (Views: 671)
Annonominity (Views: 846)

Language: