Summary of “Introduction to PHP”

PHP

PHP is a preprocessor language. It can be used to assemble pages from smaller pieces, like an erector set. It can also execute logic, such as showing a sign-up form to new users, but bypassing it for known users. PHP turns the code of the assembled pages into HTML. This is what is called preprocessing.

Comments

Comments provide an explanation in natural language. Programmers leave them for other developers in order to explain their software code. Code that is enclosed in a comment is not executed and does not affect the output.

You can make a single-line comment in PHP by writing two forward slashes //. Code on the same line after these characters will be commented out. In order to remove a comment, you need to delete //.

// I am comment text. I do not affect the program
// like the code below
// require('path_to_file.php');

Syntax

Syntax governs word order and the rules for writing words. The computer uses syntax in order to understand what we write. Every programming language has its own syntax.

Commands

A command is an instruction to a program to perform some action.

The require command adds code from the specified file to the page where require is used:

require('path_to_file.php');

PHP has other commands for including files. For example, include. You can learn more about this command in the specification.

Each command must be written on a new line, and each line must end in a semicolon: ; . This is how we tell PHP that we are finished writing one command and that what comes after is another command.

Concatenating files

When you add files to PHP, they are concatenated together. When a require command is encountered during the execution of a script, it is replaced with the contents of the included file in the exact order in which the require commands occur.

require('header.php');
require('content.php');
require('footer.php');

In the example, the content markup will be added to the header layout, and then the footer markup will be added to them.

PHP tags

For the PHP code to work, it must be placed inside PHP tags:

<?php // Opening PHP tag
// Some sort of PHP code
?>    // Closing PHP tag

Tags work like a signal. When we use them, we are saying something like “Pay attention: there is PHP code inside”.

Sometimes the closing tag can be omitted. For example, when we work with a script that only contains PHP code. If we embed a PHP code fragment in HTML, then the closing tag must be used. This is how we mark the boundaries of the PHP code inside the template.

Variables

Variables provide a way to save information under a specific name. A variable declaration is used to write the variable name when it is first mentioned. When we write information to a variable, we say that we are assigning values.

$name = 'Semyon';
// We declared the variable $name
// We assigned the variable the value 'Semyon'

The variable name must begin with the dollar sign $. The next character may be either a letter or an underscore. The name should be human-readable and describe what is contained in the variable.

Templates

Page templates store code placeholders. A template can be static or dynamic, that is, it can include variable data.

Adding PHP to the page markup

// Full notation
<p><?php echo($name); ?></p>

// Short notation
<p><?= $name ?></p>

These two options work in the same way. The only difference is in the number of characters.

Database

A database is a system that stores site information in an organized manner.

Working with an address

The address bar is a special field in the browser where the user can enter a the address (URL) of a page on the Internet.

The address has a special part that is called the “query parameters”. The query parameters are entered after the question mark.

https://www.gloevk.com/product.php?product_id=1

You use the $_GET command to get information from the address.

For example, if you would like to get the value product_id using $_GET, you would write $_GET['product_id'].


Continue