Simple php question: how to include library?

Col

New member
I have written a php library but the problem is I don't know how to use functions from it on my pages.

Is include("library.php") the best/right/only way to do it?
Because I only wish to use functions from it, not write it to the client's page.

Thanks for your help.
(New to php as you can probably tell...)
Thanks Erato, I'll give it a go and take a look at the links.

And yes, I am a Java developer, amongst other things ;)
 
personally, I like using:

require_once("path/to/your/file");

NOTE: the difference between 'require' and 'include' function is that if the file cannot be found and inserted efor whatever reason then if you use 'include' the program will continue to execute and possibly with problems because it could not include the external file. With 'require' on the other hand, the execution of the program terminates since it "requires" the file to continue. When you call the '_once'
version of the functions ('include_once' or 'require_once' it means that if the same file has already been 'included' or 'required' either in the same program or some other file you may have included or required then it won't cause problems. Otherwise, including the same files with ('include' or 'require' more than once in the same program will cause problems.)

So suppose I'm working in file called 'file1.php' and it is in the directory 'directory1' which is sitting in the document root. Then the absolute path to file1.php is: '/directory1/file1.php'. Now suppose you want to include a file called 'file2.php' which sits in director1 and you want to include it inside file1.php. Then you would make this call inside 'file1.php' :

require_once('/directory1/file2.php');

but also, since both file1.php and file2.php are both in the same directory you could provide an absolute path (i.e. just give the name of the file to the require_once function ) like so:

require_once('file2.php');

Note: this only works if 'file2.php' is in the same directory as 'file1.php'.
 
Back
Top