Results 1 to 8 of 8
  1. #1

    Simple question about php functions

    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.

  2. #2
    Join Date
    Jan 2004
    Location
    NJ, USA
    Posts
    288
    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.

  3. #3
    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.

  4. #4
    Join Date
    Jan 2004
    Location
    NJ, USA
    Posts
    288
    Quote Originally Posted by lexington View Post
    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.
    They definitely don't do that, so you're good to go

  5. #5
    Join Date
    Aug 2001
    Location
    Orange County, CA
    Posts
    532
    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.
    Jeff Standen, Chief of R&D, WebGroup Media LLC. - LinkedIn
    Cerb is a fast and flexible web-based platform for business collaboration and automation. http://www.cerbweb.com/

  6. #6
    Join Date
    Jan 2005
    Location
    Manchester, UK
    Posts
    194
    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.
    █ Software developer, web designer, and general clever-clogs.
    █ C#, XHTML, CSS, PHP, MySQL, MsSQL, T-SQL, Photoshop & more

  7. #7
    Join Date
    Jan 2004
    Location
    NJ, USA
    Posts
    288
    Quote Originally Posted by jstanden View Post
    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.
    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.

  8. #8

    Weird math problem

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

Similar Threads

  1. ASP - converting over some simple PHP functions
    By the_pm in forum Programming Discussion
    Replies: 2
    Last Post: 04-17-2007, 11:51 AM
  2. Replies: 5
    Last Post: 06-13-2005, 06:56 PM
  3. Easy Functions [Functions Tool Kit] PHP Coders Needed!
    By DiverseFlow in forum Other Offers & Requests
    Replies: 0
    Last Post: 03-14-2005, 12:40 AM
  4. REALLY basic PHP question about functions
    By kenfused in forum Programming Discussion
    Replies: 7
    Last Post: 08-01-2003, 02:51 PM
  5. PHP/Mysql: Functions inside Functions involving Queries
    By Helios in forum Programming Discussion
    Replies: 1
    Last Post: 10-24-2002, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •