3.1. Scripts

The PHP programming language is considered a scripting language, so programs written in it are called scripts. The main difference between traditional programs and scripts is that the scripts work only in a specific environment and use the resources of this environment.

For example, the scripting JavaScript programming language works primarily in Web browsers, Visual Basic for Applications only in the Microsoft Office environment. Using these programming languages, it is impossible to create a program that works without an appropriate environment.

In the case of PHP, the Web environment (Web server, database server, mail server, etc.) acts as such an environment. It is PHP that receives requests from the client, ensures their parallel execution and sending data. The PHP script receives all the information about the request and executes the request, and then sends the data back to the server.

Comment! However, the PHP programming language allows the creation of programs that operate independently of the Web server, but in this form it has not received any widespread use.

One of the main features of the PHP programming language is the fact that its code can be interspersed with the HTML code. In order for the PHP interpreter to distinguish between HTML and PHP codes, the latter consists of special tags, between which are constructions and operators of the PHP programming language.

Listing 3.1 provides a classic example that displays the phrase “Hello, world” to the browser window using the echo construct. The contents of Listing 3.1 should be placed in a file with the .php extension, for example, in the index.php file.

Listing 3.1. The simplest PHP screen. Lndex.php file

<!DOCTYPE html>
<html lang="en">
<head>
<title>The simplest PHP script</title>
<meta charset='utf-8'>
</head>
<body>
<?php
echo "Hello, world!";
?>
</body>
</html> 

The echo construct prints one or more lines to standard output. As a result of the script, the phrase “Hello, world!” Will be displayed in the browser window. When working with server-side programming languages, such as PHP, it should be remembered that the scripts located between the tags are executed on the server.

The client receives only the result of the PHP code, which can be easily seen by viewing the source code of the HTML page.

<!DOCTYPE html>
<html lang="en">
<head>
<title>The simplest PHP script</title>
<meta charset='utf-8'>
</head>
<bоdу>
Hello, world!
</body>
</html>