Summary of “Introduction to PHP”
PHP
PHP is a scripting language often used on web servers. 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 follows these rules to interpret our code. 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.
Write each statement on a new line for clarity, and end these statements with 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
PHP can assemble a page from several files. As a script runs, each require loads and executes the specified file. The files’ output appears in the order in which the require commands run.
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 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 store information under a specific name. In PHP, assigning a value to a new variable creates it. The same assignment syntax can also change a variable’s value later.
$name = 'Semyon';
// We created $name and assigned it 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 contain reusable markup. 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 the field in a browser where you can enter the address (URL) of a web page.
A URL can include query parameters after a question mark. Each parameter has a name and a value.
https://www.gloevk.com/product.php?product_id=1$_GET contains the values of query parameters from the URL.
For example, to read the value of the product_id parameter, write $_GET['product_id'].
Continue