PHP language definition and features
What is PHP
Let’s start by defining exactly what PHP is. It is difficult to give a complete picture of this language in one sentence, so we will describe it with the following three characteristics:
- PHP is a hypertext (HTML) preprocessor.
- PHP is a server-side programming language.
- PHP is a scripting, interpreted programming language.
Why you need PHP
Let’s explain each of the definitions and find out in which tasks PHP will be useful.
PHP’s main job is to bring HTML pages to life.
Regular HTML pages are static. Static (or immutable) means that after the page has been created and uploaded to the site, each time this page is accessed, the browser will show it to any user unchanged.
But this is not always enough.
Almost always, users come to the site for information that changes all the time, and you need to display its current state. For example:
- show the exchange rate;
- tell the weather for tomorrow;
- display the counter of visits to the page.
If you use only HTML, then you will not be able to solve such problems. This is where we need PHP. It accepts an incoming request from a web server, executes a script, and returns the result to the web server in the form of a finished HTML code. The server sends this result to the browser to the user, which in turn displays it to the user. After that, you can see the fresh exchange rate, the weather, and anything else.
PHP allows you to modify a web page on the server just before it is sent to the browser. Let’s see how it works. PHP can execute code – so-called scripts. During execution, PHP can change or dynamically create any HTML code that is the result of script execution. The server then sends this code to the browser. At the same time, the browser does not know how this page was formed – statically laid out by a layout designer, or dynamically created with the participation of PHP. It doesn’t matter, because the browser always works only with what it received from the server.
Let’s remember that a script is a server-side program that runs in response to a request from the browser.
Now we can understand why PHP is called the hypertext preprocessor. When a browser requests a page with a URL that ends in .php, the web server calls PHP and asks it to execute the script from the file at that URL.
The execution of a script is also called its interpretation, and PHP itself is called an interpreter.
You can practice creating dynamic pages with PHP in this trainer.
Where PHP is used
The main scope of the PHP language is the web, that is, the sites that we visit every day through a computer or smartphone browser. It is important to understand that the web is not the entire Internet, but only the largest and most popular part of it. In addition to the web, e-mail, instant messengers, file-sharing services, network games and much more work through the Internet.
Almost every website on the internet runs on PHP. This language is great for any dynamic websites, including:
- social networks;
- blogs and forums;
- online stores;
- browser games.
Installation and use
In our course, we will be working with PHP 8 version.
Where to download PHP?
We recommend using the OpenServer software build. After downloading and installing on your computer, you will have a fresh version of PHP, a web server, a database, and many other programs and tools for convenient work. We talk more about installation and use in the first lecture.
What does a PHP script look like?
A typical PHP script is a set of expressions. Each expression starts on a new line and ends with a semicolon.
An expression is a statement that tells the PHP interpreter to perform a single action, such as adding two numbers or displaying information on the screen. Let’s look at the simplest scenario. It will display one line on the screen: “Hello World!”:
<?php print(“Hello world”);
It is important to note that any PHP script must begin with this line: <?php – this is how we inform the web server that PHP code is located further in the file.
In our example, the script consists of just one statement: print(“Hello world”);
Here we are asking the print function to print our text, “Hello World!” to the screen. This can be thought of as a subject and a predicate in Russian, where the print function is the predicate, and “Hello, World!” – subject.
As we already know, PHP can be embedded in static HTML files and modified with instructions. Here’s what the same example would look like with text output, but nested inside the HTML:
<title>Our first php page</title>
<h1><?php print(“Hello world”); ?></h1>
Note that in this example, we have used an additional fragment – ?>. With it, we tell the server where our PHP script ends. In case our code is the last one in the document, and nothing follows after it, this fragment is not required.
Let’s figure out what any programming language consists of. Like the languages we speak, a programming language is also built from building blocks, which make up the body of knowledge called human language.
Each language has its own rules and constructions, following which we express our thoughts and make them understandable to another person. In programming, everything is exactly the same. But instead of the human language, we use the PHP programming language, and the PHP interpreter acts as our interlocutor. Therefore, in order to express our thought, we must make it understandable for the interpreter.
Variables are the backbone of any programming language. While writing the code, we will constantly work with variables. Understanding the concept of variables is very simple.
A variable is a container that contains data, just like a drink is contained in a cup.
Any information that we will use in the code must first be stored in a variable.
A variable must have a name, so a variable always consists of a name and a value. A value is any information that is stored inside a variable.
For example, we can ask a page visitor to enter their age, and then use this value for other purposes – to find out the year of birth or to show the age of the page itself.
How to work with variables
Any variable must first be declared, that is, given a name and a value.
In PHP syntax, a variable name is written in Latin characters, but the first character must always be a dollar sign $, followed by the name.
It is not allowed to start a variable name with a number, and also to use any values other than alphabetic characters and underscores.
Examples of valid variable names: