PHP alone is not enough to create a dynamic website.
You actually need a web server software.
So here let's have a high level overview of where does exactly PHP come into play, and what are the
other things that are required to have a working website.
Now let me zoom in a little bit so we can clearly see every step along the way.
Everything starts with a client.
Now client can just be a person browsing the website through the web browser, or just accessing your
server application in any other way.
And the client makes a request.
So PHP alone can't handle the requests for that.
You need some kind of a web server.
Typically, nginx is a popular choice.
Other popular choices is for example, the Apache web server or caddy baths.
So if the code of our application is written using PHP, this server software alone cannot run it.
It needs to pass the PHP code to PHP itself.
And the way to do that for web servers is the Fastcgi.
Now this sounds complex, but the shortcut means fast common gateway interface, and that's just a protocol
that web server software use to communicate with external programs.
That's nothing specific to PHP, it's just a way web server software like nginx, but also Apache and
others use to communicate with external programs, and PHP is one of those external programs.
Next we've got something called PHP, FPM and this shortcut means PHP Fastcgi Process Manager.
And that's just an implementation of this fast CGI protocol for PHP.
And it manages a couple of processes called worker processes that can do something with the PHP code.
So once the web server is passing the request on to PHP Process Manager, one of those worker processes
can pick up the PHP code, and only then we get to this blue box where the PHP interpreter runs.
So PHP is an interpreted language.
This means that PHP code is not turned into a binary.
It needs to be interpreted every single time.
Only then it can be run.
That differs, for example, from programs that you might have on your computer, whether Windows or
Mac OS, which are compiled and you probably don't even know in which language they were created.
So that's not the case with PHP.
PHP is interpreted.
Now let's try to quickly understand how does it happen and how can this process of PHP interpretation
be optimized?
Next up, let's try to quickly understand how the PHP interpreter works.
So we just need to understand that we humans are able to comprehend complex structures.
And this also includes code which typically can include complex operations.
But that's not really the case with computers.
Computers need to translate everything into simple list of operations that should be performed one by
one.
And that's the job of the interpreter.
So it starts with a tokenizer.
And I'm going to use a natural language that we all speak as an example, whether that's English, German
or French.
So when you take a look at the text in any of the languages, I mean English, German, you see some
random letters?
It might seem so at least.
But if you look closer, then there are words.
Words are separated by spaces.
And that's what tokenizer is doing with the programming code.
So tokenizer is changing just a group of characters into tokens.
So it's like distinguishing words.
And tokens can be keywords.
Some special characters like parentheses or curly braces.
So the first step is breaking the code into the tokens.
Next we've got parser.
The parser is creating something called abstract syntax tree, which is just a tree representation of
your code flow and what should happen.
And the third step is the actual compiler that creates this simple operations called opcodes.
So only after we have those opcodes our PHP code is actually being run, so it's simple enough for computers
to understand at this point.
And it is being executed by the executor, which in PHP is just called Zend Engine.
Now, as you might see here, this process can be further optimized.
You can use something called Opcache, which is just a way to keep the compiled opcodes between subsequent
requests inside this opcache.
Now, keep in mind that everything I'm explaining here is quite complex.
Compilers are hard.
It's a complex problem.
I am telling you this at this point so that you just know more or less how it all works.
Yths.
Finally, this executor is just running the PHP code that we were so painstakingly writing, and the
executor is just interacting with all the external things like the database to fetch the data or store
it as typically you will be using some kind of a database.
Let's say even for a simple blog written using PHP, you might want to fetch those blog posts from the
database.
So you might also work with the file system, whether this is with images or some kind of documents,
whatever your application will need.
And then you should also know that the PHP functionality is extended by extensions.
So extensions is not something that you or I would be creating.
It's created by the community and typically it's written in the C language.
So the idea behind extensions is that PHP language and runtime can be kept quite pure.
And if there are any needs for PHP to work with any external technology, whether that's database or
you need to handle image processing, then this functionality is built into Extensions.
So PHP ships with a set of some popular, often used extensions by default, but if you require some
additional functionality, then there might be extensions existing for that very purpose, which, as
I've said, also lets PHP be kept clean.
All right, so then we are just going back.
So once the PHP interpreter will do its job by tokenizing, parsing, compiling and executing PHP code,
then it will just get some output.
That's typically an HTML page, but it can be anything.
And then it will be returned back through the worker back to the web server, which then sends this
as a response to the client, which typically, as I've said, is a web browser.
So the person visiting your website or page will see the output that he wanted.