Can Php be used to include part of a file?

  • Thread starter Thread starter HomeAndFamily.biz
  • Start date Start date
H

HomeAndFamily.biz

Guest
I have several php codes that I use many times throughout my script. For example, I have code1, code2, & code 3 located several times within several different files.

Can I just somehow place all of these codes in 1 file and then call upon the place that they are in that file?

With all the objects, if statements, arrays, variables, etc (all of this confuses me), is there some way to create something that will work?
 
Depending on what the code snippets do, you could create functions or classes for them.

If you want to put them in a common.php file, put them in functions. In your other files, put
include_once('common.php');
at the top. Then just call the functions.

This is old school though. PHP allows classes for a reason now. You should read about Object Oriented Programming.
 
Yes you can.

include('/path/to/file/');
include_once('/path/to/file/');
require('/path/to/file/');
require_once('/path/to/file/');

The include functions will try to replace the function with the file, if it fails, PHP will report an E_WARNING

The require functions will do the same as the include functions, but if they fail then PHP will throw an E_ERROR

The _once part will only allow the file to be included once.
 
Back
Top