SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: PHP Functions

  1. #1
    SitePoint Addict Chris Roane's Avatar
    Join Date
    Jul 1999
    Location
    Helena, MT
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Functions

    Hi,

    If you include a fairly large file full of functions, will php read all the contents of every function right when the page loads OR will it basically just read the function names and then it will parse each specific function when you call the respectively?

    What I am getting at is will a large include file take just as long to load as an include file that use the exact same functions but only has those functions that were used in the file you included it on?

  2. #2
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My Guess would be that a file with less functions will load faster. But the amount of tiem taken into consideration is negligible.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  3. #3
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats an interesting question... freddy, do you have a benchmark for this?

    actually, i think it loads the entire file, and not just that function... afterall, it does have to scan through the entire code to find out where the function is written
    i dunno...

  4. #4
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that's an interesting question.

    For a board I'm writing at the moment I also use a file funtions.php with more than 500 lines... I include that file in each file I use on the board...
    But if php has read the file, doesn't php cache the file? Or I am wrong?

  5. #5
    SitePoint Addict Chris Roane's Avatar
    Join Date
    Jul 1999
    Location
    Helena, MT
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am almost sure it doesn't read the file, because it will only produce an error when the function is called. If it went through every line, wouldn't it produce the error even if the function wasn't called?

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope once you include a file it will produce errors if there are errors in the included file.

    PHP won't cahce the file unless you have caching enabled on your server. include() also reevaluates the included file(every line) each time the include() statement is present.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris, you will get, as freddy meant, parse errors when you include a file even if you don't use the function. i know that you meant ones when you call the function. (something like "Invalid number of arguments to preg_replace() in functions.php on line 87".) you don't get those until you call the function b/c they're runtime errors, whereas parse errors are compile time errors.

    to get back to your question. yes PHP compiles ALL the code every time the file is loaded. no caching takes place (unless maybe if you're using the Zend Cache?).

    to give you an idea, i have a functions file w/ 280 lines (6 something K) and took most of the functions out which made it 75 lines (1.19K). it lowered the page creation time by about .004 to .005 seconds (from .01 to .005 secs.). so you can see it does make a diff. - twice as fast on my simple page.

  8. #8
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    include() actually puts the code from that file into the file that called it. So all of the code from includefile.php is put above the rest of the code in the file that made the include(); call.

  9. #9
    SitePoint Addict Chris Roane's Avatar
    Join Date
    Jul 1999
    Location
    Helena, MT
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh okay. That makes more sense.

    Thanks for explaining all that to me.

  10. #10
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm guys.. how about the require() function???

    I have a php include that I call with the require() It brings me a header function and some other functions.. Will the server read this or only use it when nessecary .. ( I don't need it with all my php pages but have it standard in my template :o)

    I'm a bit confused now because I thoughed that that was the difference between include and require..

    Cheers,


    Peanuts
    Last edited by peanuts; Aug 12, 2001 at 10:55.
    the neigbours (free) WIFI makes it just a little more fun

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From the manual

    Unlike include(), require() will always read in the target file, even if the line it's on never executes. If you want to conditionally include a file, use include(). The conditional statement won't affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.
    from: http://www.php.net/manual/en/function.require.php

    This little paragraph holds the key to your question.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm if I understand correctly then it is better to use require when dealing with stuff like an access.php

    and include is better for templates etc..

    Greetz Peanuts
    the neigbours (free) WIFI makes it just a little more fun

  13. #13
    SitePoint Addict Chris Roane's Avatar
    Join Date
    Jul 1999
    Location
    Helena, MT
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it would just depend on how your template and access is setup.

    If you need included files in if's, then just use include().

  14. #14
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Chris Roane
    If you need included files in if's, then just use include().
    correct. that's how i'm doing it.

  15. #15
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, don't forget the difference between parsing and compiling. Parsing happens before compilation. And, while I'm not going to pretend I know much about how the zend engine compiles, being an interpreter, it compiles on the run.

    Parsing -> the interpreter (the Zend engine) parses the code to check that it is well formed and legal syntax. That is, it performs a sanity check that it will be able to compile it. If there are any syntax errors you will be thrown back a parse error. No execution of the script will have occcured.

    Compilation -> This is where the interperter (the Zend engine) interprets your code and the state of the application at that time (for example, it interprets and decides the types of variables currently in memory, etc - because while PHP may be loosly typed this is because the interpreter is making type decisions on the fly) and compiles machine code and sends it off to the CPU using the instruction set for the machine's particular architecture, for the CPU to actually get down and do the doda.

    Again, I don't know much about the Zend engine, but, in general, compilers can also go through an intermediary stage known as pre-processing. This is where the interpreter/compiler goes through the code and optimizes it. This involves substituting macros such as "true" and "false" and replacing them with their true values (int)0 and (int)1. This may also involve breaking down functions - and rather than keep them as functions, which requires creating a new block of memory on the application's memory stack when the function is called, it might just replace the function call by placing the function's code in-line in the main-line of the application. Those are just some general observations about how compilers interpret source code, and a little off track. Sorry


Bookmarks

Posting Permissions

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