WEBVTT

00:00.320 --> 00:08.600
So far, every single example or statement that we've learned, we did the work in one single file.

00:08.630 --> 00:15.980
Now, this would be pretty hard to maintain in any project that has more significant size.

00:16.010 --> 00:22.130
That's why typically you would like to distribute your PHP code across multiple files.

00:22.160 --> 00:28.880
Now then you would need to somehow make sure that those files work together.

00:28.880 --> 00:36.470
So as you already know, PHP is not a compiled language, so every single time you'd like to use some

00:36.470 --> 00:39.800
code from another file, you need to load it.

00:39.800 --> 00:46.700
And in this video we're going to learn about two instructions or actually four instructions that let

00:46.700 --> 00:47.600
you do that.

00:47.600 --> 00:51.590
Load other PHP files and code inside them.

00:51.590 --> 00:55.010
They are called include and require.

00:55.040 --> 00:59.510
And for that let's create another folder first.

00:59.510 --> 01:05.120
Let's call it require and only then we're going to create some files inside.

01:07.480 --> 01:15.100
For this example, let's just use um, quite typical use case of storing the configuration in a separate

01:15.100 --> 01:15.910
file.

01:16.000 --> 01:25.600
Let me create a config.php file that will just store our configuration.

01:25.600 --> 01:29.620
And other than that let's create a main PHP file.

01:29.620 --> 01:32.530
Which job would be to run something.

01:33.010 --> 01:35.350
So let me jump back to the config file.

01:35.350 --> 01:39.550
And here let's just store some configuration options.

01:39.580 --> 01:41.590
Obviously that's all made up.

01:41.590 --> 01:43.150
We are just having an example.

01:43.150 --> 01:52.150
So maybe we would like to connect to a database so we can start the database host and the database user.

01:54.430 --> 01:56.860
Now let's jump to the main PHP file.

01:56.860 --> 02:01.570
So this file is to Bo is supposed to do something important.

02:01.570 --> 02:06.130
And we just want to keep the configuration separate.

02:06.130 --> 02:13.430
You know, not to have files that are excessively big without a reason.

02:13.430 --> 02:21.830
If we can divide the code into different files with some specific purpose, why won't we do that?

02:21.980 --> 02:30.470
So here, let's say I'd like to connect to the database, but obviously we're just gonna simulate that.

02:30.470 --> 02:41.510
Now in this example I will just output the db host and the db user variables.

02:43.370 --> 02:52.610
And if I now go to this require folder and I try to run the main PHP file, well we've got errors that

02:52.610 --> 02:54.650
we've got undefined variables.

02:54.680 --> 02:56.120
Now obviously that's the case.

02:56.120 --> 03:02.480
Those variables are not defined here but they are defined in this config PHP file.

03:02.510 --> 03:11.720
Let's see how can we get this file to be included inside our main file, which contains our app logic.

03:12.740 --> 03:22.330
So the first way is to just include the file using the include statement, where you just need to add

03:22.330 --> 03:25.390
the or provide the relative path to this file.

03:25.390 --> 03:28.030
So in it is in the current directory.

03:28.030 --> 03:30.580
So I can just do php config.

03:30.820 --> 03:40.720
PHP and if I run this script again now we have the values of both variables which is localhost and route.

03:44.080 --> 03:48.340
Now another way is to use the require stand statement.

03:48.340 --> 03:51.670
If I run this script, this seems to be the same.

03:51.670 --> 03:53.500
Now there has to be a difference.

03:53.500 --> 03:56.740
Otherwise those two statements would not exist.

03:56.740 --> 04:04.300
So the difference is that with the include statement, if the included file won't exist, we might get

04:04.300 --> 04:12.370
a warning, but the script will try to continue to run, but with require the application would just

04:12.370 --> 04:13.210
crash.

04:13.210 --> 04:19.690
So let me rename the config file to something else so we make sure it wouldn't be found.

04:19.690 --> 04:23.400
And first let's see the include way.

04:23.670 --> 04:27.570
So let's rerun the script again.

04:27.960 --> 04:36.900
And as I've said, we've got the PHP warning that the file could not be found and nothing really happens.

04:36.900 --> 04:43.320
We got more warnings that the variables also weren't found, they weren't defined.

04:43.320 --> 04:51.540
And if you have the warnings disabled in PHP, which is possible, the script will just run.

04:51.540 --> 04:57.840
It's obviously wrong and it won't do whatever it needs to do, but it will try to, you know, somehow

04:57.840 --> 05:02.760
cope with the situation and do whatever is possible.

05:02.790 --> 05:06.030
Now it's different with require.

05:07.590 --> 05:11.250
Now let me clear the console and run it again.

05:11.310 --> 05:15.690
So here we get a fatal error.

05:15.690 --> 05:19.530
So we couldn't open this config PHP file.

05:19.530 --> 05:22.890
And basically the script stop at this point.

05:23.160 --> 05:30.830
So you can see that there are no warnings about undefined variables in this echo statement, because

05:30.860 --> 05:35.510
stopping at this point means that this line wasn't even executed.

05:35.510 --> 05:37.400
We stopped at this line.

05:37.400 --> 05:39.020
The file wasn't found.

05:39.050 --> 05:40.850
It's a critical error.

05:40.850 --> 05:42.710
So the script has stopped.

05:42.740 --> 05:50.480
Now, when you need to include crucial information like the configuration for a database, it's obviously

05:50.480 --> 05:56.660
much better idea to use require because you know it is essential for the script to run.

05:58.130 --> 06:04.610
Now let's rename this file back to this original config php name so it works again.

06:06.350 --> 06:10.730
Now the require and include are just normal statements.

06:10.730 --> 06:18.440
You can use them how many times you want, and sometimes this might have some unexpected effects, because

06:18.440 --> 06:25.700
in those PHP files you not only can have the configuration and some variables defined, you might also

06:25.700 --> 06:31.760
have some code running and requiring this file two times would then cause.

06:31.760 --> 06:41.760
This included required code to run twice, and for such cases you just might want to use require ones

06:41.760 --> 06:48.750
or include ones well, they are the same as require and include respectively.

06:48.750 --> 06:56.100
They just make sure that if you've included or required certain file already, it won't happen again,

06:56.100 --> 07:00.240
even if there is an instruction that tries to do that.

07:00.330 --> 07:08.010
So typically it is safer to rely on either require one's own or include ones.

07:08.010 --> 07:12.150
And as a quick reminder, so you remember the main point.

07:12.150 --> 07:20.280
The main difference between require and include include will let you run your script even if the file

07:20.280 --> 07:27.960
doesn't exist or couldn't be found for any reason, and require will just crash if the file can't be

07:27.960 --> 07:28.710
found.

07:28.710 --> 07:31.080
So use require for any.

07:31.110 --> 07:39.540
Let's call it mission critical files that maybe have configuration to the database or any other credentials.
