Page 1 of 1
how to create a function library
Posted: Wed Feb 02, 2011 4:20 am
by dentrite
hi, i am a n00b php duke, i am programming my site and has created several functions that would be used on various pages of my site, i do not want to add or define those function on every page, so is there any way to create a "header file like" page in php through which i could access functions simply by including the file name? i have tried the function include("path") but it doesn't works, it just shows the output after processing the page. help.
Re: how to create a function library
Posted: Wed Feb 02, 2011 4:36 am
by cpetercarter
Put your functions in a file called, eg, functions.php, and include it at the start of any page where you want to use any of the functions. You need, of course, to make sure that you specify the correct path from the calling script to functions.php. Also, put only functions, and nothing else, in functions.php - if you put any directly executable code in functions.php it will run whenever you include the file. Make sure that you don't include functions.php twice by mistake eg if one of your pages includes another, otherwise php will throw a fatal error and tell you that you have already defined these functions and cannot do so a second time.