asa_carter wrote:
I have read that require_once should be used instead of include. Why? Doesnt require exit the script on failure? So would inlclude not be better?
Typically, if I'm including something I'm really requiring to reuse some functions. And since my code is depending on those function definitions being loaded then I'd rather have a fatal error if they cannot be found right away.
asa_carter wrote:
Would it be more efficient to put a lot of commonly used functions such as database connection, retrieving user details etc in one include file and then calling the functions instead of having each in separate include and then calling the include?
The granularity of release is the granularity of reuse. So if you have lets say you have 5 database functions that you've honed over the years and really want to use in every project cause they make your life simple. The optimum approach is to keep them in their own file so they can evolve on their own. To do that effectively the file (library) would be under version control (eg: CVS). That way your DB functions and your templating functions and image manipulation functions, etc, can all evolve independently. Moreover, I don't think having extra files hurts performance...php is still gonna churn over the same number of lines of code either way.
So now comes the time to code your app and you don't want 20 require_once statements at the top of every page. Well, there are lots of ways around that, but what I typically do is have an init.php file that contains nothing but require_once() statements that will load all "common" files I will need on each request, so wherever I need them, I just require_once('init.php');