Chapter 5. Reusing PHP Code

Include a Code File

Your Web site scripts may need to perform a task repeatedly, such as displaying the company address or storing data in a database. Efficient development dictates that you write PHP code to perform a certain task and use the same code whenever the task is required.

One method for reusing code is PHP include files. You can store as many lines of code as you need in a file, separate from your script, and insert (called include) the file into the script wherever you need it.

PHP provides four statements for inserting a file into a script: include, include_once, require, and require_once. You can use the following format for all four, passing a string or a variable:

include(path/filename);

The include/require ...

Get PHP & MySQL®: Your visual blueprint™ for creating dynamic, database-driven Web sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.