Beginners guide for PHP development
The command in the php -v console now works for you.
Now our task is to create the first php file and try to execute it.
In order to do this, create a test folder on the computer and go to it using the cd command (see video).
Before creating a php file, you need to understand that this is a regular text file that has the php extension.
In order to create a php file, you can create a simple text document. For example, we create an index.txt document and simply change its extension to php.
This file can be opened in the text editor that is installed on your operating system, or in some code editor in which you are working.
It is still empty for us and we can already run it through the command console:
The result of executing this command will not return anything yet.
In order to write the first php command, you need to create a special construct that tells the php language interpreter that this is a php command, and not some plain text.
In order to enable the php command mode, the following construction is written:
Everything that will be inside this structure, the php language will perceive as commands of the php language and will execute them.
The first command we’ll look at is a simple statement that prints something on the screen.
When this code is executed, the following line “Probe” will be displayed in the terminal.
If you write it like this, then the echo command will be perceived as plain text, because this is outside the php construct.
Try to create such a simple file and we will move on to the next lesson.
So, we have created the first PHP document, which we launched through the console and got some result.
Now let’s get a little familiar with the syntax of the PHP language. How can you execute commands in this language and what features are there?
We already know that in order for any PHP command to be executed, it must be inside the following construct:
We go further. The next syntactic rule of the PHP language is that all language commands are separated from each other by a semicolon “;”.
echo “Command 1”;
echo “Command 2”
If the semicolon sign is not put between the commands, we will get an error when executing the script.
At the end, after the very last php command, a semicolon is allowed.
One more moment. If no HTML code is displayed after the <?php … ?> construct, this construct may not be closed. But, after the last command, you must definitely put a semicolon in this case.
It will look like this:
echo “Command 1”;
echo “Command 2”;
But, if any HTML content is expected to be placed after the PHP construct, this construct must be closed.
These are the syntax features of the PHP language. These are the very basics, but this will be enough to get you started.
Like any programming language, PHP has variables. Let’s see how we can create them.
In order to create a variable in PHP, first of all, you need to put the $ sign. Next, you need to come up with a name for the variable. The name must be indicated in Latin letters. Next, put an equal sign and indicate the value that the variable will take.
In PHP, it is not necessary to specify the type of data that will be stored in this variable.
Here is the code for an example:
$var1 = 123;
If the number is specified without quotes, then the variable will store a number, not a string.
$var2 = ‘123’;
By adding quotes to the value of a variable, we make the value type “string”. As if the text is stored there, not a number.
Quotes can be either single or double.
$var3 = 1.25;
The variable var3 will store a floating point number.
$var4 = true;
The var4 variable stores a value of a boolean type (true or false).
So far we have created 4 variables and this is enough for now.
This is how variables are created in PHP.
If we now execute this script, where we declared variables, then nothing will be displayed as a result. Creating variables in no way affects the program itself. These variables are simply stored in the program’s internal memory.
In PHP, the equal sign, which is used in mathematics, is not an equals sign. This is a special command called the assignment operator.
Suppose we have some variable $a.
$a = 1;
The variable a has been assigned the value 1.
$a = 1;
$a = 3;
What value do you think will be output in the end in this case?
If you check, you will see the value 3.
The = sign is the assignment operator in PHP. The code is executed sequentially: from top to bottom, from the first line to the last.
First, the variable a is assigned the value 1, then we override this value with the value 3. And, as a result, we store the number 3 in the variable a.
This is the meaning of work.
In addition to the assignment operator, PHP has
$a .= 3;
Then we will merge the number 1 with the number 3. PHP will work with these values not as numbers, but as strings.
Keep in mind this is a feature of working with the assignment operator. It happens that the program code can be very long. You can assign a value to the variable $a = 1 in some place and further down the code, you can override this value and, as a result, your program may not work the way you intended.