Correct syntax of PHP
Let's figure out what any programming language consists of. Like the languages we speak, a programming language is also built from the building blocks that make up the body of knowledge called the human language.
Each language has its own rules and constructions, following which we express our thoughts and make them understandable for 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 foundation of any programming language. As we write the code, we will constantly work with variables. The concept of variables is very easy to understand.
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 the page visitor to indicate their age, and then use this value for other purposes - to find out the year of birth or show the age of the page itself.
How to work with variables
Any variable should first be declared, that is, give it a name and assign 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 alphabet letters and underscores.
Examples of valid variable names:
Examples of invalid names:
age- the dollar sign at the beginning is forgotten;
$42- starts with a number;
$my-age- contains a hyphen.
An empty variable won't be very helpful. So let's put something there right away. This is called an assignment operation.
Assigning information to a new variable looks like this:
$favorite_color = "green";
The equal sign in PHP is an operator and always means an assignment operation.
Here we have written the word “green” into a variable called favorite_color .
Note that we have quoted the word green. Quotation marks are always strictly necessary when it comes to using text. But if the variable is not text, but a number, then the quotes are not needed.
$favorite_number = 42;
We have learned how to store information in variables. Now let's see how to access this information in our script.
After all, when we try to remember a new phone number in our head, we do it not just like that, but in order to call it.
This means that we remember information in order to return to it in the future and apply it for some action - to make a call, write a message, and the like.
This logic fully works in programming as well. Having saved the information once, it can be used in the future to perform various actions. This action can be displaying this information on the screen.
Let's rewrite the already familiar script to use variables.
An operator in programming is something that takes one or more values.
For example, in arithmetic there are such operators: +, -, /, *. What does addition, subtraction, division and multiplication mean. An operator always works only in pairs with two values, for example, adding two numbers (operands) or multiplying them by each other.
PHP supplements the operators already familiar to us from arithmetic with several new ones:
- the familiar assignment operator
=, which is used to assign a value to a variable;
- very useful comparison operators:
<- equal, not equal, greater or less;
%- operator of the remainder of dividing one number by another. For example:
5 % 2 == 1; // true.
Sometimes, depending on the condition, you need to perform different actions. The if statement is used for this. For example, we asked the gender of the visitor to our page, saved it to a variable, and now we want to display a different greeting, depending on whether it is a man or a woman. The
ifand operators come in handy for this
Another kind of expression is an arithmetic expression. The expression
$amount = 2 + 2will return 4.
When do you need to know the result of an expression?
Expressions are especially useful in conditions, that is, when we want to perform or not perform some action, depending on the result of the expression. Expressions can also be combined with each other in such a way that several separate expressions end up being evaluated as one.
Let's assume that on our site we want to show a certain picture only to male visitors over 18 years old. Earlier, in the script code, we have already got and saved the year of birth and gender of the visitor into the variables
Let's write an expression and a condition to implement this behavior:
Many of us have heard something about algorithms in computer science lessons at school. Unfortunately, not all school knowledge remains with us after graduation. Nevertheless, understanding what an algorithm is, and the ability to build these very algorithms are very important skills, without which it will not be possible to solve even relatively simple problems.
In simple terms, an algorithm is just a very detailed work plan. We all plan something during our lives: a vacation, an event, our independent study, and the like. What distinguishes the algorithm from a simple list of steps is the existence of conditions and repetitive actions. If you are able to create a good, detailed algorithm for implementing, say, some feature on the site, then you can assume that half of the work has already been done!
Let us analyze, for example, one algorithm of medium complexity. You need to add a feedback form to the site. The user can fill out this form, indicate his contact information there and write a message. Information from the completed form is sent to the website owner by e-mail. This is how the algorithm for this task will look like:
- Show the form.
- Check that the form has been submitted.
- Make a list of required form fields.
- For each field from the list, check that it is filled in.
- If the field is empty, show an error and do not submit the form.
- If all fields are filled in, generate an e-mail message.
- Send a message to the site administrator's address.
Most programs always have a separate settings window where you can specify all the main parameters of this application. PHP also has its own settings, only they are changed not through the interface, but by editing a special file - php.ini. All PHP settings are set in the php.ini file. From what we will be interested in first of all is the error management mode, connecting additional features, setting up sessions and cookies.
This file is located in different paths in different operating systems. The easiest way is for users of the OpenServer assembly - there php.ini can be opened from the main menu.