Introduction to PHP

This is an online PHP language course designed for general education. It is available for free on this site. Texts should not be used in any paid course without written confirmation. Used samples from the whole website can be used without further restrictions in your application.

Upgrade from HTML

PHP retains all the features and functionality of a pure HTML document, just adding new writing and deployment options. Great, right?

From the development of static HTML pages, you already know that the code consists of tags that are defined as keywords enclosed in pointed brackets (for example, <b> Hello! </b> is bold Hello! .

PHP is inserted into the HTML page as a <? Php and?> Tag, inside which another application logic is written. Importantly, PHP has its own syntax (the rules by which the code is written) and, unlike HTML, is not error tolerant.

We will learn specific examples of how and what each brand means. To begin with, it is important to understand the general principles of how PHP works on a server and how code is handled.

Communication between the user and the server

For an ordinary HTML page, it works like this: The user sends a request (requests a specific HTML page), the server looks at the disk and sends back exactly what is stored. Nothing special happens and nothing more to expect. The pages will be static, without any possibility of server interaction. But if we add PHP, the wonders will start to happen: The user requests the page again. The server opens the file on disk, but sees that it contains not only pure HTML, but also special tags that indicate PHP script. First, it evaluates them and then sends what PHP has generated. PHP code evaluation is performed by default every time the page is loaded, in the future you will learn how to cach the code (save compiled for faster processing).

PHP script processing differs from C / C ++

You may have learned to use C or C ++ at school. PHP is directly based on the C language syntax and C language is used inside the kernel, so it is good to know some common features and differences.

The basic principle of common compiled programs (which run locally directly on your computer or smartphone) is to load application logic into the operating memory, which communicates directly with the operating system, which receives input from the user and then displays the program outputs. Importantly, the program runs in isolation from start to finish.

PHP runs with every page render request, reloads all code and data every time and then exits. PHP scripts therefore literally have a beeping life and normally there are only tens of milliseconds.

The advantage of this approach is a higher degree of isolation - if anything goes wrong, the next page load reloads. This approach, on the other hand, has a higher performance requirement because, for example, we need to reconnect to a database, read files from a disk, and so on.

In the future you will learn that you can keep PHP scripts loaded in RAM using the OP Cache extension, which is set by most new servers (starting with PHP 7.1).

Downloading other PHP scripts

Relatively often people ask how to download foreign PHP scripts from the server and look at their source code. This query is preceded by the consideration that the HTML of the page can be easily viewed in a web browser.

The answer is that PHP scripts cannot be downloaded. The reason is that the PHP code is first evaluated on the web server and then the generated HTML code (or other output) is sent to the user's browser. Therefore, only the output from the PHP script can be downloaded, not the script itself.

How does "generate to HTML" work?

The PHP script must always be in a PHP file.

Until now, your web page creation consisted of creating huge folders, full of files, ending in .html. Now it will be far fewer files. In the extreme case, it may be a single file.

Next chapter (First script)

Back to Learn PHP main menu