How PHP scripts work
Surely you are familiar with the principle of operation of radio-controlled cars. Press forward – the car goes, press to the left – the car changes the direction of the wheels.
PHP works the same way. You write commands one by one, and PHP executes them sequentially.
PHP programming is like operating a car
What is PHP script
A PHP script is a plain text file with a .php extension . Exactly the same as html and css.
But there is one important difference:
Scripts are only run through http requests in the browser. This means that to run the script script.php, you need to write site.ru/script.php in the browser address bar
If the script doesn’t work, check the current URL. If there is something like file: /// D: /openserver/domains/site.com/script.php – this means that you are trying not to go to the site page, but to open the PHP file itself in the browser. It is not right.
Web servers are most often configured so that when you visit the main page (for example, site.ru ), the index.php or index.html file located in the root of the site is automatically launched.
In the root folder of the site, create a file called index.php and open it in your text editor. If there is an index.html file, you need to delete it.
If you don’t have a text editor yet, I recommend the lightweight and free Notepad ++ , it is much more convenient than the built-in Windows notebook.
Be sure to keep an eye on the script encoding. Encoding should be either UTF-8 without BOM (if available in your editor), or just UTF-8 .
Choosing an encoding in Notepad ++
Outputting numbers and strings in PHP
PHP commands are written between <? Php and ?> Tags , for example:
<?php echo 25 ?>
The echo command is responsible for displaying information on the screen. After the operator, the value to be output is indicated.
To display text, you need to specify it in single or double quotes:
<?php echo 'WORLD BEST TUTORIAL' ?>
PHP commands are separated by semicolons. For readability, it is customary to write each command on a new line:
<?php echo 'PHP '; echo 'CLASS '; echo 'ROOM'; ?>
Result in the browser:
PHP CLASS ROOM
HTML output in PHP
HTML code can be mixed with PHP commands:
<h1><?php echo 'php class room' ?></h1>
Also, HTML code can be substituted into a PHP string:
<?php echo 'PHP<br>class room'; ?>
Result in the browser:
PHP class room
We can combine PHP code and HTML tags in any way:
<div><?php echo 'Строка1' ?></div> <span><?php echo '<strong>Строка2</strong>' ?></span>
Functions in PHP
A function is a command that performs some kind of action, such as calculations, file management, etc.
Defining a function is easy – it has parentheses after its name:
<?php phpinfo(); ?>
The phpinfo () function displays information with the current PHP settings.
Some functions expect to be passed some value. For example, the ceil () rounding function expects a number that it will round:
<?php echo ceil(91.5); ?>
The ceil () function takes the 91.5 we passed , processes it (rounds to 92), and returns the result. And the echo command gets the value returned by the function and displays it on the screen. The result of this script will be 92 .
Functions will be discussed in detail in a separate lesson .
The need for a closing tag in PHP
If there is no HTML or any other output after PHP commands, then we can not use the closing tag ?> :
<?php echo 'test';
Shorthand syntax for opening tag in PHP
Instead of <? Php echo, you can simply write <? = . Useful for those cases where we need to substitute a value between HTML tags, for example:
<div><?= 150 ?></div>
Alternatively, we can replace the <? Php tag with a shorter version <? :
<? echo 'Вася' ?>
Using the <? caused a lot of controversy among PHP developers. The creators of the PHP language put an end to this question: starting from PHP 7.4, the short tag is declared obsolete, and in PHP 8 it will no longer be.
For this reason, I don’t recommend using the short <? …
This does not apply to the <? = Tag , you can safely use it in your projects.
Single line comments in PHP are written after the # or // character and are valid until the end of the line:
<?php # single line comment echo 'are you ready'; // Single Line Comment echo 'we will teach you amateur to mature';
Multi-line comments are placed between / * and * / :
<?php /* Multi line comments */ echo 'India is great country';
Please note that multi-line comments cannot be nested within each other:
<?php /* # single line Comment // single line Comment */ /* /* Multi line Comment */ */
A PHP script is a text file with a .php extension, in which PHP commands are specified between the <? Php and ?> Tags .
It is important to keep an eye on the encoding of the scripts and choose either UTF-8 without BOM or just UTF-8.
To display any values in the browser, use the echo command, or the shorthand syntax <? = :
<?php echo 15; ?>
<?= 15 ?>
If there is no output after the PHP code, the closing tag ?> Can be omitted.
PHP functions are specified with parentheses at the end:
You can also add single-line and multi-line comments in PHP:
# Single line comment // Single line comment /* Multi Line Comment */