Setting up a Basic PHP framework
When using PHP, once of the most useful functions is the ‘include’ function, which allows you to include all of the code from one php file in another. using this, we can set up some good systems for keeping our code organized, and making it easier to edit and use.
To start off, let’s make and save 5 blank files, then we will discuss what is going to go in each one.
- functions.php
- definitions.php
- header.php
- footer.php
- template.php
functions.php
This one is pretty self-explanatory, any functions that come up in your site should be written here. This way, you will have them all in one place, and they will be able to be called anywhere (we’ll get to this part in a bit).
definitions.php
This is where you want to declare all constants, and all variables that are used frequently throughout the site. If you are using MySQL, you’ll also want to put your database information here, and make your main MySQL connection here.
header.php
This is the real meat of this framework. You want to include functions.php and definitions.php, so that you can use all of the information there. You also want to have the HTML code for the head, and the header here.
footer.php
here, you want to close the html and body tags, include any code that runs at the end of your script (for example, Google Analytics or Kontera). You may also want to include a call to the mysqli_close() function (not nessecary, but it can’t hurt).
template.php
a template you can use for any of your pages. It should look something like this:
<?php include "header.php"; ?>
//whatever code you have for the site
<?php include "footer.php"; ?>
And there you go! You’re code will be easier to read, easier to edit, and easier to use.