Currently we've got two files indexed up p HP and Article doppie HP both accessing the database by first
connecting to it using this code at the top.
You might have noticed that we've duplicated this code in both files.
This is a bad idea.
Having repeated cold like this what if we change the database password for example we'd have to make
sure we changed it in both of these files and if we continue to repeat this code we'd have to change
it all over our website.
Potentially in many more files in addition to being time consuming if we forgot one we'd break our website.
The solution to this is to extract this repeated cold out into a separate file and then include this
separate file in any script that needs it.
To include one file in another MP HP.
There are actually four functions available when our cold calls.
They include statement we pass in the path and file name of the file we want to include and that file
is evaluated and included in our script.
This means that any code inside the included file is executed immediately just as though the code it
contains was inserted directly at that point in the first script.
Require is the same as include except that if the file to be included is not found the script stops
with include if the file doesn't exist.
A warning will be shown but the script will keep going
require once and include Once the same but these will check to see if the file has already been included
before.
So it won't do it again depending on what the included file contains including a file more than once
could cast an error using require once and include once are slightly slower as HP needs to keep track
of how many times a file has been included.
I've never really found a need for these two as if you need to include a file more than once.
Then it would probably be more advantageous to argue nasia code better using functions are classes which
will learn about later on.
So let's remove this repeated cold at the top of these two files.
First let's create a file containing this cold and Carl it databased up here.
P.
Remember it still has to have the p h.
P ext as it's p h.
P code will cut this code from both files.
And paste it in the new one.
Don't forget to include the p p opening tag at the top.
Now we can include this file in the other two.
Let's start with include passing in the name of the new file we just created.
In the browser our index page still works as before.
Let's see what happens when we try and include a file that doesn't exist by making a mistake in the
final that M..
Now in the browser we get lots of error messages but the script has continued after the include line
even though the include failed.
Let's change this to require.
But still with the wrong file name.
And now in the browser we get what's called a fatal error which stops the script the Require has failed
because it couldn't find the file and the script stops executing.
That's the difference between include and require if the file to be included isn't found include.
We'll keep going but require will stop.
For this reason I almost always prefer to use require.
I can't think of an instance when it would be o k for my code to continue if the file I was trying to
include wasn't found.
So let's leave it with the choir.
But correct the error and also put this in the article doppie it p script like that.
Now our index page is working again.
And also the article page but we've removed the repeated database code and were including it from this
separate file.
You might have noticed that this isn't the only repeated code that we've got.
Were also repeating ourselves at the top and at the bottom of the x t AML.
So let's create a new file for the top of our hvm out and call it header copy it p
in here will put the repeated h t l code then another file for the bottom of the X Team L which we'll
call footer appear.
P.
And in here we'll put the repeated h.t. email code from the bottom.
Now even though these just contain S T L.
For now we might want to add some common p h Pico to them later.
So it's best to keep the P HP file extension.
Now in our indexed up p h.
P we can replace this code.
By including the header.
P P file instead.
Not forgetting the p h.
P tags in here because we're inside it's team out.
The same with this code from the footer like that and will do the same for this cold.
In the article file first the header s email here.
And again at the bottom for the footer to email like that.
And in the browser nothing has changed.
It all works as before.
Now however we're not repeating ourselves and we have the added advantage of our code being a little
bit clearer and easier to understand.