WEBVTT

00:01.460 --> 00:05.930
PHP alone is not enough to create a dynamic website.

00:06.020 --> 00:09.560
You actually need a web server software.

00:09.560 --> 00:18.470
So here let's have a high level overview of where does exactly PHP come into play, and what are the

00:18.470 --> 00:22.400
other things that are required to have a working website.

00:23.780 --> 00:29.540
Now let me zoom in a little bit so we can clearly see every step along the way.

00:30.020 --> 00:32.900
Everything starts with a client.

00:32.930 --> 00:41.090
Now client can just be a person browsing the website through the web browser, or just accessing your

00:41.090 --> 00:43.580
server application in any other way.

00:43.580 --> 00:46.730
And the client makes a request.

00:46.730 --> 00:51.320
So PHP alone can't handle the requests for that.

00:51.320 --> 00:53.840
You need some kind of a web server.

00:53.870 --> 00:58.010
Typically, nginx is a popular choice.

00:58.040 --> 01:04.520
Other popular choices is for example, the Apache web server or caddy baths.

01:04.670 --> 01:14.620
So if the code of our application is written using PHP, this server software alone cannot run it.

01:14.650 --> 01:19.840
It needs to pass the PHP code to PHP itself.

01:19.840 --> 01:25.570
And the way to do that for web servers is the Fastcgi.

01:25.600 --> 01:35.770
Now this sounds complex, but the shortcut means fast common gateway interface, and that's just a protocol

01:35.770 --> 01:41.800
that web server software use to communicate with external programs.

01:41.830 --> 01:49.990
That's nothing specific to PHP, it's just a way web server software like nginx, but also Apache and

01:49.990 --> 01:58.450
others use to communicate with external programs, and PHP is one of those external programs.

01:59.260 --> 02:10.540
Next we've got something called PHP, FPM and this shortcut means PHP Fastcgi Process Manager.

02:10.540 --> 02:17.950
And that's just an implementation of this fast CGI protocol for PHP.

02:18.220 --> 02:28.870
And it manages a couple of processes called worker processes that can do something with the PHP code.

02:28.870 --> 02:37.690
So once the web server is passing the request on to PHP Process Manager, one of those worker processes

02:37.690 --> 02:47.080
can pick up the PHP code, and only then we get to this blue box where the PHP interpreter runs.

02:47.470 --> 02:50.560
So PHP is an interpreted language.

02:50.560 --> 02:55.150
This means that PHP code is not turned into a binary.

02:55.150 --> 02:59.170
It needs to be interpreted every single time.

02:59.200 --> 03:01.270
Only then it can be run.

03:01.270 --> 03:06.940
That differs, for example, from programs that you might have on your computer, whether Windows or

03:06.940 --> 03:15.330
Mac OS, which are compiled and you probably don't even know in which language they were created.

03:15.330 --> 03:17.730
So that's not the case with PHP.

03:18.120 --> 03:20.130
PHP is interpreted.

03:20.160 --> 03:28.890
Now let's try to quickly understand how does it happen and how can this process of PHP interpretation

03:28.920 --> 03:30.510
be optimized?

03:31.800 --> 03:36.390
Next up, let's try to quickly understand how the PHP interpreter works.

03:36.390 --> 03:43.680
So we just need to understand that we humans are able to comprehend complex structures.

03:43.680 --> 03:49.320
And this also includes code which typically can include complex operations.

03:49.320 --> 03:52.050
But that's not really the case with computers.

03:52.080 --> 03:59.160
Computers need to translate everything into simple list of operations that should be performed one by

03:59.160 --> 03:59.700
one.

03:59.700 --> 04:02.010
And that's the job of the interpreter.

04:02.010 --> 04:04.170
So it starts with a tokenizer.

04:04.170 --> 04:10.680
And I'm going to use a natural language that we all speak as an example, whether that's English, German

04:10.680 --> 04:12.000
or French.

04:12.390 --> 04:19.980
So when you take a look at the text in any of the languages, I mean English, German, you see some

04:19.980 --> 04:21.030
random letters?

04:21.030 --> 04:22.680
It might seem so at least.

04:22.680 --> 04:26.670
But if you look closer, then there are words.

04:26.670 --> 04:29.070
Words are separated by spaces.

04:29.070 --> 04:33.990
And that's what tokenizer is doing with the programming code.

04:33.990 --> 04:41.820
So tokenizer is changing just a group of characters into tokens.

04:41.820 --> 04:45.840
So it's like distinguishing words.

04:45.840 --> 04:48.120
And tokens can be keywords.

04:48.120 --> 04:53.100
Some special characters like parentheses or curly braces.

04:53.340 --> 04:57.810
So the first step is breaking the code into the tokens.

04:57.840 --> 04:59.970
Next we've got parser.

05:00.000 --> 05:08.010
The parser is creating something called abstract syntax tree, which is just a tree representation of

05:08.010 --> 05:11.040
your code flow and what should happen.

05:11.040 --> 05:18.630
And the third step is the actual compiler that creates this simple operations called opcodes.

05:18.630 --> 05:27.320
So only after we have those opcodes our PHP code is actually being run, so it's simple enough for computers

05:27.320 --> 05:28.850
to understand at this point.

05:28.850 --> 05:35.630
And it is being executed by the executor, which in PHP is just called Zend Engine.

05:35.660 --> 05:41.090
Now, as you might see here, this process can be further optimized.

05:41.090 --> 05:50.060
You can use something called Opcache, which is just a way to keep the compiled opcodes between subsequent

05:50.060 --> 05:53.780
requests inside this opcache.

05:53.810 --> 05:58.640
Now, keep in mind that everything I'm explaining here is quite complex.

05:58.640 --> 06:00.800
Compilers are hard.

06:00.800 --> 06:02.390
It's a complex problem.

06:02.390 --> 06:09.500
I am telling you this at this point so that you just know more or less how it all works.

06:09.980 --> 06:11.810
Yths.

06:12.500 --> 06:21.710
Finally, this executor is just running the PHP code that we were so painstakingly writing, and the

06:21.710 --> 06:29.420
executor is just interacting with all the external things like the database to fetch the data or store

06:29.450 --> 06:33.650
it as typically you will be using some kind of a database.

06:33.650 --> 06:41.270
Let's say even for a simple blog written using PHP, you might want to fetch those blog posts from the

06:41.270 --> 06:42.290
database.

06:43.040 --> 06:48.950
So you might also work with the file system, whether this is with images or some kind of documents,

06:48.950 --> 06:51.380
whatever your application will need.

06:51.380 --> 06:58.520
And then you should also know that the PHP functionality is extended by extensions.

06:58.520 --> 07:03.170
So extensions is not something that you or I would be creating.

07:03.200 --> 07:09.290
It's created by the community and typically it's written in the C language.

07:09.290 --> 07:17.930
So the idea behind extensions is that PHP language and runtime can be kept quite pure.

07:17.930 --> 07:25.880
And if there are any needs for PHP to work with any external technology, whether that's database or

07:25.880 --> 07:33.850
you need to handle image processing, then this functionality is built into Extensions.

07:34.030 --> 07:43.270
So PHP ships with a set of some popular, often used extensions by default, but if you require some

07:43.270 --> 07:49.900
additional functionality, then there might be extensions existing for that very purpose, which, as

07:49.900 --> 07:54.430
I've said, also lets PHP be kept clean.

07:57.340 --> 08:00.880
All right, so then we are just going back.

08:00.880 --> 08:10.240
So once the PHP interpreter will do its job by tokenizing, parsing, compiling and executing PHP code,

08:10.240 --> 08:13.540
then it will just get some output.

08:13.540 --> 08:17.410
That's typically an HTML page, but it can be anything.

08:17.440 --> 08:25.270
And then it will be returned back through the worker back to the web server, which then sends this

08:25.270 --> 08:30.910
as a response to the client, which typically, as I've said, is a web browser.

08:30.910 --> 08:37.360
So the person visiting your website or page will see the output that he wanted.
