how to create a function library

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dentrite
Forum Newbie
Posts: 16
Joined: Thu Jun 17, 2010 7:10 am

how to create a function library

Post 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.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: how to create a function library

Post 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.
Post Reply