Basic PHP syntax. PHP: Alternative syntax of control structures Example of a simple html page with PHP code

Let's look at a few basic and most frequently encountered elements so that in the future we don't have to look at the set of symbols and wonder what it is, what it's for, and how it works.

And in the next lesson we’ll start writing scripts and see what comes of it.

Example 1. A simple script for displaying on the monitor screen.

The first thing that catches your eye is:

— opening and closing tags, or PHP markers. They are always present at the beginning and end of the code.

PHP supports several variations of opening and closing tags.

— a complete (classic) version of the start and end tags, recommended for constant use.

— a shortened version, works when the short_open_tag directive is enabled (enabled by default). This is a directive built into php that determines whether shortening of php tags is allowed.

However, it is better to use the classic version, especially when working with XML code, to avoid ambiguity in interpretation.

…….. - advanced option, available at any time without additional settings (rare)

— a special type for displaying a simple expression, is essentially a shortened version of , convenient when writing a script consisting of a single expression, works when the short_open_tag directive is enabled.

Any PHP code always starts and ends with these tags. Everything that is inside them is called a script, or script, or program.

error_reporting (- 1 ) - instructions that enable the display of all error warnings in the script you wrote. It is advisable to use it constantly during training.

echo is an instruction that displays what is written in it on the monitor screen. This can be text, numbers, html markup, that is, everything that is on the web page.

The text must be enclosed in quotation marks, and numbers can be in the form of actions, and the result of this action will be shown on the monitor. Here's a calculator!

+ - addition
- - subtraction
* - multiplication
/ - division
% - remainder during division
- before the line - negation

are called php arithmetic operators. There are many more different operators. These are string, and logical, and assignment operators, and several other types of operators, but more on all of them a little later.

Each expression ends with a semicolon.

Example 2. Variables and constants.

$var and $vAr are variables

Variables are stores of information. All information that you put into the program is stored in variables. They always begin with a dollar sign, and the name is given by the programmer, that is, you.

The variable name can consist of any number of letters, numbers, and underscores, but cannot begin with a number. Variable names are case sensitive. The same word written in capital letters will be perceived as two different names.

The name always begins with a small letter. The number of variables in the script is not limited.

= - assignment operator

define("I",1000,true); — constant

"I" is the name of the constant
1000 — constant value
true - the case of letters is taken into account; if you put false instead, then the case will not be taken into account.

A constant in PHP is always defined by the define() function, and it stores data that in no way changes while the program is running. The name of a constant is always written in capital letters.

Example 3. Comments.

It is considered good form to provide detailed comments to scripts.

After the // and # signs, the browser ignores everything before the end of the line.

Example 4. Function.

function - a function that processes a certain number of actions aimed at obtaining a specific result.

arr_max — function name
($arr) - arguments (input data) of the function

There are more than 4000 functions in PHP, but since they are all divided into categories, it is not difficult to find the one you need in the directory.

Example 5. Control structures.

Constructs are used to control step-by-step execution of code.

$flag1 , $flag2 , $flag3 — variables
if and else are conditional operators, that is, they check the condition - false or true.
&& is a logical operator, read as a connecting conjunction “and”.
||
- a logical operator, read as a separating conjunction “or”

echo - instruction output to the screen


Now we will know at least a few basic names and purposes of the script elements. In fact, there are still plenty of them in PHP, and in the future, both in theory and in practice, we will consider them in detail.

Turn
-Abram, you know Einstein is coming to us.
-What kind of fraer is this?
-This is the same guy who invented the theory of relativity.
-For God's sake. I don't know the theory of relativity.
-How do you not know the theory of relativity? I will explain.
We pull out three hairs from you - is that too much?
- We throw it in your soup - is that a lot?
-Yes.
-Understood?
We pull out three hairs from you - is that too much?
-I’ll explain for the sake of understanding. You're sticking your nose in my ass. Your nose is in your ass, my nose is in your ass. The expression is the same - the sensations are different! Understood?
-And what... does he want to come to Odessa with us with this joke?

Note: the adaptive version of the site is activated, which automatically adjusts to the small size of your browser and hides some details of the site for ease of reading. Enjoy watching!

Hello, dear readers, and soon PHP developers;) Today's Site on blog article!

is devoted to the basics of PHP: syntax features, data output, variables, and working with errors. During the series of PHP lessons, I will try to tell you as much interesting and useful stuff as possible, while trying not to lengthen the articles.

PHP blocks

The first thing you should know is that PHP code should always be enclosed in PHP tags:

You can also use:

First, every statement (expression) must end with a semicolon; For example:

Secondly, all the same things can be written in one line; between instructions in one line you can put as many spaces as you like, including no spaces at all:

Third, the PHP instruction can be broken into several lines:

Result:

We observed that the browser interpreted each newline as a normal space, which is what we should expect.

Fourth, PHP, like all programming languages, has comments. There are 2 types of them in PHP: single-line and multi-line.

// - single-line comment # - this is also a single-line comment /* Your comment */ - multi-line comment

There is an opinion that for a good programmer, comments should make up 30% of the entire code. However, redundancy of comments is also completely unnecessary; you should not leave comments like “here I looked out the window and scratched my nose.”

Outputting data in PHP

Data output in the PHP programming language is carried out using two main language constructs:

The difference is that when executed, print returns one, while echo returns nothing. If you don’t know how exactly to use this, then use echo and don’t bother, especially when using echo you can do this:

By the way, the same thing could be written like this:

This is what I meant in the previous article when I mentioned that .

Variables in any programming language are used to store some information within themselves, that is, a variable is our vessel. We can put one thing there first, then remove the first and put the second, or we can leave the first and add a second (and a third, etc.).

Variables in PHP begin very symbolically - with a dollar sign $, followed WITHOUT a space by either a Latin letter or an underscore (a number cannot be the first character in the variable name). Further, the variable name may contain both Latin letters and numbers, and the same underscore character. For example:

Third, the PHP instruction can be broken into several lines:

Variable names are case sensitive! That is, $Name, $naMe, $name are three completely different variables. If we want to put something new into an existing variable, the old value of this variable will be automatically erased:

Third, the PHP instruction can be broken into several lines:

Naturally, we can pass the value of one variable to another:

Third, the PHP instruction can be broken into several lines:

However, the value of the $_blog1 variable remains in it.

Unlike strings, when entering numbers into a variable, quotes are not needed:

$name = 45;

As when putting a variable into a variable:

$name = $_blog1;

After finishing the code on the page, all PHP variables are automatically deleted. But there are rare cases when we need to forcefully delete a variable before the code ends. The unset function is used for this:

Third, the PHP instruction can be broken into several lines:

Dealing with errors in PHP

Now we have smoothly moved on to the topic of errors in PHP. As you can see, we are accessing the $name variable, which was previously mercilessly removed - this led to the Notice. Notice should be considered a real error, although in most cases it is absolutely harmless.

PHP even tries to tell us where and what kind of mistake we made. In our case he writes:

Undefined variable: name

Which translates as “undefined variable: name” and then shows us the file and the line on which this whole incident happened:

In Z:\home\localhost\www\blog2\second-page.php on line 10

That is, in the second-page.php file on line 10. In this case, PHP got it right, but it often happens that the error is one or more lines higher, for example, when we forget to put a semicolon at the end of the next instruction: