Syntax in PHP

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 in the browser address bar

If the script doesn’t work, check the current URL. If there is something like file: /// D: /openserver/domains/ – 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, ), 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:

    echo 'PHP ';
    echo 'CLASS ';
    echo 'ROOM';

Result in the browser:


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:

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:


The phpinfo () function displays information with the current PHP settings.The result of the phpinfo function

Some functions expect to be passed some value. For example, the ceil () rounding function expects a number that it will round:

    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 ?> :

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.

PHP Comments

Single line comments in PHP are written after the # or // character and are valid until the end of the line:

# 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 * / :

Multi line comments

echo 'India is great country';

Please note that multi-line comments cannot be nested within each other:

        # 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 <? = :

    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

Creating a .htaccess file on Windows

What is .htaccess file

Htaccess file is an Apache web server configuration file that can be used to override some Apache settings for an entire site or a specific folder.

How to create a .htaccess file on Windows ?

If you try to create an .htaccess file in Windows operating systems , the result will be a “File name must be entered” error.

Error creating a .htaccess file on windows.

To work around this error, you need to create an empty text document and then rename it to .htaccess. (with a dot at the end). After renaming, the extra period will disappear.

If you are using a dedicated text editor, you can most likely create your .htaccess file through it. Instructions for Notepad ++ users:

  • Open Notepad ++
  • Click File -> Save As
  • In the new window, select the file type of all types (Eng. The All types ), enter the .htaccess and save.

Sample .htaccess file

Some of the most popular directives are site encoding and error control settings. Let’s specify them in the .htaccess file:

# Charset
AddDefaultCharset UTF-8

# Errors
php_value error_reporting -1
php_flag display_startup_errors on
php_flag display_errors on

In this example, we have specified the following directives:

  • AddDefaultCharset – indicates the encoding of the site.
  • error_reporting – indicates which level errors to catch (for display on the site and in logs), and which to ignore ( -1 means to catch all errors).
  • display_errors and display_startup_errors – whether to display errors in the browser ( on – display, off – do not display). Usually turned on during development and turned off on a live site.

Now the .htaccess file can be placed in the root folder with the site.

Installing OpenServer

We will going to Installing OpenServer but before this we should know about What is openServer ?

OpenServer contains many programs necessary for website development, and allows you to conveniently manage them.

In particular, OpenServer contains Apache and Nginx web servers (read as “Engine X”), MySQL and PostgreSQL DBMS. Their versions, as well as the version of PHP itself, are easily switched in the control panel.

OpenServer analogues

OpenServer is one of the most popular builds of software for web developers. It’s user-friendly, intuitive, and great for beginners.

But this is not the only such product. There are a lot of analogues and the most popular of them are XAMPP and AMPPS.

Please don’t use Denwer. Once it was a great tool, but by now it is very outdated and there is no point in using it.

You can download OpenServer from the official site in the “Download” section.

Regarding the version, Basic is enough for a start, but in the future you will probably want to work with the Git version control system, which is only in the Premium version. Therefore, I recommend choosing Premium right away, so as not to waste time on migration later.

When downloading, the creator of OpenServer asks for a voluntary donation for the benefit of the project. If this is not done, the download speed will be significantly reduced. You can, of course, download OpenServer via torrents, but you need to be careful – viruses are often embedded there.

Installing OpenServer on Windows

So, let’s start the installation.

Running the OpenServer installation

Choosing an installation location. You do not need to create an OpenServer folder, it will be created by itself. Those. if you specify D: \ , then OpenServer will be installed to D: \ OpenServer \ .

Specifying the folder to unpack OpenServer

After installation, several folders and 2 files with green icons will appear in the OpenServer folder. Run the file corresponding to your operating system (x86 for 32-bit OS, x64 for 64-bit).

If you don’t know the bit depth, try running both files. Whichever one does not show an error is the one needed. 🙂

OpenServer installed folder

Select your language and confirm the installation of Microsoft Visual C ++ (may take a few minutes).

Language selection and installation of MS Visual C ++

This completes the OpenServer installation.

OpenServer setup

After installation, the OpenServer icon will appear at the bottom right of the screen as a red or green flag. Go to Settings -> Modules .

Here we are only interested in 2 parameters: HTTP (web server) and PHP. If you are just starting to learn PHP, I recommend choosing the latest versions of PHP and the Apache web server (just that, no Nginx).

OpenServer Settings Menu

After saving the settings, click the red checkbox and select Run . OpenServer will start or give an error.

To make sure the installation is correct, enter localhost / in your browser . If you see the OpenServer start page, then everything is in order.

OpenServer successful installation message

Website creation in OpenServer

It is very easy to create websites. Go to the domains folder and create a folder with the site name, for example test .

The domains folder in OpenServer

Reboot the server (click on the checkbox and the Restart button ).

Create folder and restart OpenServer

Now create an index.html file in the test folder , write any text in it and enter test / in the browser (with a slash at the end so that the browser understands that this is a domain name, not a search query).

If you see the contents of the index.html file , then everything is working correctly.

If OpenServer is not working

OpenServer must run as administrator and have access to the hosts file ( C: \ Windows \ System32 \ drivers \ etc \ hosts for Windows 7).

If OpenServer says that the 80th port is busy, most likely the matter is in the Skype settings. Go to Settings -> Advanced -> Connection and uncheck the “Use ports 80 and 443.

Disable port 80 in Skype before installing openserver

If it did not work, it is possible that other assemblies (Denwer, AMPPS, etc.) are installed on the computer that block the 80th port. If so, you need to change the port used in the settings of one of the programs.

If you are using Windows Server, you must disable all Internet Information Services (IIS).

Why PHP is needed

Before understand Why PHP is needed we should know about What is PHP ? PHP is a server-side scripting programming language designed for building websites and web applications.What you need to pay attention to:

ServerPHP scripts are executed on the server. This is how PHP differs from HTML, CSS, and JavaScript, which are executed by the browser.

We cannot just open a PHP file in a browser, as we are used to doing with HTML files. For PHP scripts to work, a special program is required – a web server.

Script. When you start programs like Excel or Photoshop, it is assumed that they can work without time constraints, expecting more and more new commands from you.

PHP works differently. PHP scripts are designed to execute a set of commands at a time. The script started, completed its task and “died”. And so on every start.

Why PHP is needed

The PHP language is needed to write programs that run on the server and perform various server-side tasks such as sending mail, interacting with a database, etc.

Let’s take a closer look at examples.

Example 1: processing forms

On almost any site you can find a form like this:An example of a regular HTML form

Forms have very different purposes: registration and authorization, ordering, paying for something, comments on the forum and many other functions.

So that’s it. With HTML and CSS, we can define the appearance of the form. With Javascript, we can add beautiful animation effects.

However, we cannot process the data contained in this form. You cannot save information about a client in a single database, authorize a user, send a letter to him by E-mail, etc.

To solve such problems, we need a special programming language that can run on the server. PHP is one such language.

Example 2: generating HTML

PHP is also used to automatically generate HTML pages.An example of a product catalog in an online store

Large online stores can have thousands of product names. We could of course create a separate html page for each product, but imagine how long it will take to create these pages.

Thanks to the combination of PHP and any modern DBMS (database management system), we can create just one html template, and PHP will already substitute goods from the database into it.

What PHP is for

The PHP language is ideal for creating websites and web applications of any complexity. Blogs, online stores, landing pages (one-page sites), APIs and other web services can all be done in PHP.

PHP is also suitable for writing console scripts. You can run them both once and at regular intervals.

As far as desktop applications are concerned, PHP has a rather meager toolkit, so in practice PHP is not suitable for writing desktop and mobile applications.

Why PHP?

These are the important points for Why PHP is needed in web development field:

  • Huge popularity . Any problem that a beginner may encounter has most likely already met with other people, which means that the solution will not be difficult to find on the Internet.
  • Support by hosting providers. Almost any hosting provider can provide hosting or virtual server with PHP already installed and configured. Some other languages ​​need manual installation.
  • Great community . The forums are home to a sea of ​​PHP developers willing to help solve particularly difficult problems.
  • Low entry threshold . PHP is easy to get started with, it has a fairly simple syntax.