Let’s talk about data types in PHP. Speaking of types, we mean the types of values.
Compared to other programming languages (for example, C/C++ or Java), in PHP the same variable can be assigned values of different types. Therefore, in PHP, types should not be assigned to a variable, but to a value. And say, for example, that the variable $var contains an integer value. This is called “dynamic typing” – the same variable can store values of different types. I agree this is very cool.
Let’s look at the following code:
<?php $var = 123; $var = 'string'; $var = 3.14;
Here the $var variable on each line changes its value, while these values have different types. In the first case – an integer, in the second – a string, in the third – a fractional number (float).
Strings can be specified using double or single quotes. But there is a difference – in double-quotes, the interpreter will try to find variables, but inside single quotes, no.
<?php $var = 123; echo "Variable value: $var";
This code will display the line:
Variable value: 123
And another example:
<?php $var = 123; echo 'Variable value: $var';
Outputs the following:
Variable value: $var
As we can see, the search for variables was not performed inside single quotes and the interpreter returned the text to us exactly as it was defined.
To display the value of a variable and use single quotes, you can “glue” strings together. This technique is called string concatenation (linking). To do this, use the operator “.” (dot) between two values, which is separated by spaces.
<?php $var = 123; echo 'Variable value: ' . $var;
Variable value: 123
It is worth noting that when using single quotes, working with strings will be faster since there will not be a search (or more correctly parsing) of variables inside the string.
Let’s now look at a code similar to the one we just wrote:
<?php $var = 123; $string = 'Значение переменной: ' . $var;
The variable $string now stores the value of the type “string”. Despite the fact that before that there was an integer in $var. This happened as a result of using the dot operator to concatenate strings. As a result of its use, you will always get a string. And, as you probably already guessed, the type that will result from some expression will depend on the operator. That is, when using the plus (addition operator), a number will always be obtained.
<?php $x = 1; echo $x + '2';
PHP is smart enough to automatically convert the string ‘2’ to a number and perform addition. Thus, depending on the operator, the operands will be first converted to the desired type, and then an operation will be performed. This is called typecasting.
Typecasting can be done independently. This is done using the following construction:
<?php $string = '123'; $numeric = (int) $string;
The variable $string is now a string, and the variable $numeric is already an integer. So far, we will not dwell on this in detail, but knowing that this is possible will not be superfluous.
Let’s go over the types that we have already examined. These are numbers (integer and fractional), strings. In addition to these, there are several other types of data. We all will not consider them now, but consider another simplest type – boolean (Boolean value). This type can have only two values: true and false. This type is used to test various conditions, and in the next lesson, we will consider its application in more detail. That’s all for types.