Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

whocares

macrumors 65816
Original poster
Oct 9, 2002
1,494
0
:noitаɔo˩
Hi all,

I have on rather large PHP function in a largish page. This function doesn't get used every time the page is loaded. Does having the function as part of the page significantly slow down the parsing of the page (as opposed to including the function only when required).

This is really more of a theoretical question as I don't care if it's either integrated or included...

Cheers,
D.
 
For the sake of organization, I will typically include a large function rather than integrate it. Speedwise, I'd be surprised if there was a difference one way or the other.
 
whocares said:
Does having the function as part of the page significantly slow down the parsing of the page (as opposed to including the function only when required).

yes, if you have the function either in the script itself or included, the engine is going to parse that function. even if it's never called. try throwing an error in the middle of the function, and then including it in a script that never calls it. it will still choke.

if you could include the function only when it's needed, you'd speed up the load time when it's not being called. i used a very large include (1,000+ lines) with multiple object classes and functions, and the majority of my script's load time was just getting through the damn include.

what i'll do sometimes, if the situation calls for it, is put the function in a separate script and call it only when it's going to be used; then use a redirect to head back to the originating script.

so, if myscript.php has a form that collects a bunch of variables, etc., then maybe that form's action is set to myscript_process.php. myscript_process.php will be just the function without any HTML wrapper, and once it's done with its thing, i'll just call header("location: /myscript.php") so that the big function is only called when i'm actually using it.
 
Another way to include it "dynamically"

PHP:
if ($timeToLoadMyBigAssFunction)
{
  include("mybigassfunction.php");
}

of course depending on your needs, you could use include_once() or require() functions instead.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.