Organising your code is one of the most important things that a web developer has to do, It makes the maintenance of the code much easier and also helps you when and if you need to debug the code. So in this part of our PHP tutorial we will look at how to organise the PHP files and codes by using PHP internal functions.

In PHP there are internal functions that helps you with organisation of the code by allowing you to copy a piece of the code directly into your code for example if you are using a database then you need to connect to it, well you could create a file call it “connect.php” and then add that to the top of the page. Or you could create an object to handle all of your functions and then add it to the top of your page then you don’t need to copy the code into the PHP files over and over again.

How you do it? There are different ways to do this:

include(filename);

filename is including the path to the file it can be a web address like “http://www.google.com/” or it can be a PHP file on your server like “connect.php” the filename should be in the “” quotations however it can be inside a variable.

filename is including the address to the file. This address can be in one of the two forms it can be absolute like “http://developerscreed.com/test_folder/connect.php” or it can be relative address like “test_folder/connect.php” or “connect.php” if it is in the same folder.

Sometimes however if you include a file you will have an error for the code that you have inside the file, please note that include function will not give you an error and it will add it as much as you need it so you can copy the same code as many times as you need but sometime using the file WILL give you problems you can solve this by using this code instead:

include_once(filename);

The above function will only add the filename code into the code if they have not been added before. This way you won’t be duplicating the code and wont have any errors that way.

As I said before include() and include_once() functions will continue executing the code even if PHP can’t add the file that you need, but sometimes the code will NEED the file to run correctly in this case you can use one of the following codes instead of using include() and include_once():

require(filename);

require_once(filename);

the same rule for filename applies to both of the above functions.