PHP is one of the most common web development languages. It is used to create sites and web applications of any complexity. from landing pages and blogs to online stores and browser games.
Its advantages are in its wide capabilities and security thanks to the closed source code. The disadvantages include the fact that the code will not work without a server with an interpreter (fixed in new versions of the language).
What tasks can be solved in PHP
To get started, create a page with the extension .php or .html (depending on the server configuration). The source code of this page contains html markup tags and directly PHP commands. They are between the <? PHP operators (it is acceptable to write <?) And?>. Here’s what the code looks like:
<?
$username = “Peter”; //Username variable
?>
<div>
<p>Hello, <? echo $username; ?>!</p>
</div>
By opening this page, the user will see the inscription “Hello, Peter!” If you are already familiar with programming, you may have noticed that $ username is a variable, and the echo command is used to display data on the screen.
PHP supports the use of functions, mathematical calculations, working with variables (as in the example above) and arrays, prescribing if-then conditions, creating objects, and much more. Most often it is used:
- to submit forms;
- work with databases;
- creating dynamic pages;
- use of sessions and cookies;
- download and file processing;
- create images;
- parsing.
Forms
If you are familiar with html, then you know that with it you can’t just get the information entered by the user. That is, you can create a beautiful form, but without PHP or any other similar language, all the data will simply disappear when it clicks the “Submit” button.
The <form> tag has two attributes: action and method. The first indicates the page where the data will be sent and the second indicates the transfer method. There are two of them:
- POST is a safe but slow method. It is used to transmit confidential information: passwords, logins, addresses, and so on.
- GET is a simple but vulnerable method. You could see links like index.php? Page = 8 & category = 2. Information is written directly to the link and anyone can change its value or share it, so it’s better not to transmit anything secret and take care of additional security.
The login and password variables will be passed from the form to the auth.php page. Depending on the method, they will fall into the super array $ _POST or $ _GET. Further on this page, you can do anything with them, including checking their availability in the database.
Database
A database is connected to the site in PHP. This greatly simplifies development, because all information will be stored in tables, and not in the code itself. So faster to edit and add data to the site.
For example, user information. If it weren’t for the databases, I would have to store all the authorization data in the code, and this is not the best option, because then the download would take forever.
Let’s continue with the authorization example. The auth.php page receives a login and password. She can then query the database to see if such a combination exists.
Of course, this is a very simplified version of the code, which can hardly be called protected. In fact, you need to perform many checks to exclude the possibility of SQL injection – this is when the user tries to enter incorrect information in order to gain access to the database.