So far, every single example or statement that we've learned, we did the work in one single file.
Now, this would be pretty hard to maintain in any project that has more significant size.
That's why typically you would like to distribute your PHP code across multiple files.
Now then you would need to somehow make sure that those files work together.
So as you already know, PHP is not a compiled language, so every single time you'd like to use some
code from another file, you need to load it.
And in this video we're going to learn about two instructions or actually four instructions that let
you do that.
Load other PHP files and code inside them.
They are called include and require.
And for that let's create another folder first.
Let's call it require and only then we're going to create some files inside.
For this example, let's just use um, quite typical use case of storing the configuration in a separate
file.
Let me create a config.php file that will just store our configuration.
And other than that let's create a main PHP file.
Which job would be to run something.
So let me jump back to the config file.
And here let's just store some configuration options.
Obviously that's all made up.
We are just having an example.
So maybe we would like to connect to a database so we can start the database host and the database user.
Now let's jump to the main PHP file.
So this file is to Bo is supposed to do something important.
And we just want to keep the configuration separate.
You know, not to have files that are excessively big without a reason.
If we can divide the code into different files with some specific purpose, why won't we do that?
So here, let's say I'd like to connect to the database, but obviously we're just gonna simulate that.
Now in this example I will just output the db host and the db user variables.
And if I now go to this require folder and I try to run the main PHP file, well we've got errors that
we've got undefined variables.
Now obviously that's the case.
Those variables are not defined here but they are defined in this config PHP file.
Let's see how can we get this file to be included inside our main file, which contains our app logic.
So the first way is to just include the file using the include statement, where you just need to add
the or provide the relative path to this file.
So in it is in the current directory.
So I can just do php config.
PHP and if I run this script again now we have the values of both variables which is localhost and route.
Now another way is to use the require stand statement.
If I run this script, this seems to be the same.
Now there has to be a difference.
Otherwise those two statements would not exist.
So the difference is that with the include statement, if the included file won't exist, we might get
a warning, but the script will try to continue to run, but with require the application would just
crash.
So let me rename the config file to something else so we make sure it wouldn't be found.
And first let's see the include way.
So let's rerun the script again.
And as I've said, we've got the PHP warning that the file could not be found and nothing really happens.
We got more warnings that the variables also weren't found, they weren't defined.
And if you have the warnings disabled in PHP, which is possible, the script will just run.
It's obviously wrong and it won't do whatever it needs to do, but it will try to, you know, somehow
cope with the situation and do whatever is possible.
Now it's different with require.
Now let me clear the console and run it again.
So here we get a fatal error.
So we couldn't open this config PHP file.
And basically the script stop at this point.
So you can see that there are no warnings about undefined variables in this echo statement, because
stopping at this point means that this line wasn't even executed.
We stopped at this line.
The file wasn't found.
It's a critical error.
So the script has stopped.
Now, when you need to include crucial information like the configuration for a database, it's obviously
much better idea to use require because you know it is essential for the script to run.
Now let's rename this file back to this original config php name so it works again.
Now the require and include are just normal statements.
You can use them how many times you want, and sometimes this might have some unexpected effects, because
in those PHP files you not only can have the configuration and some variables defined, you might also
have some code running and requiring this file two times would then cause.
This included required code to run twice, and for such cases you just might want to use require ones
or include ones well, they are the same as require and include respectively.
They just make sure that if you've included or required certain file already, it won't happen again,
even if there is an instruction that tries to do that.
So typically it is safer to rely on either require one's own or include ones.
And as a quick reminder, so you remember the main point.
The main difference between require and include include will let you run your script even if the file
doesn't exist or couldn't be found for any reason, and require will just crash if the file can't be
found.
So use require for any.
Let's call it mission critical files that maybe have configuration to the database or any other credentials.