Data types in PHP
Hello everyone! Today we will talk about data types in PHP. When we talk about types, we mean the types of some values.
Compared to other programming languages (for example, C / C ++ / Java), in PHP the same variable can be assigned values of different types. Therefore, in PHP, types should not be related to a variable, but to a value. And say, for example, that the $var variable contains an integer value. This is called "dynamic typing" - the same variable can store values of different types. Agree, this is very cool.
Let's take a look at the following code:
<?php $var = 123; $var = 'string'; $var = 3.14;
Here the variable $var changes its value on each line, while these values have different types. In the first case - an integer, in the second - a string, in the third - a floating point number (float).
Strings can be specified using double or single quotes. But there is a difference - the interpreter will try to find variables in double quotes, but not inside single quotes.
<?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';
Will output the following:
Variable value: $var
As we can see, the search for variables was not performed inside the single quotes and the interpreter returned the text to us exactly as it was defined.
You can concatenate strings to display the value of a variable and still use single quotes. This technique is called string concatenation. To do this, use the "." (dot) operator between the two values, separated from them by spaces.
<?php $var = 123; echo 'Variable value: ' . $var;
Will output the line:
Variable value: 123
It is worth noting that when using single quotes, working with strings will be faster, since there will be no search for variables inside the string.
Let's now look at some code similar to the one we just wrote:
<?php $var = 123; $string = 'Variable value: ' . $var;
The $string variable now stores a string value. Even though there was an integer in $var before. This happened as a result of using the dot operator to concatenate strings. Using it will always result in a string. And, as you probably already guessed, the type that will result from any 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';
Will output the result:
PHP is smart enough to automatically convert the string '2' to a number and perform the addition. Thus, depending on the operator, the operands will first be converted to the required type, and then the operation will be performed on them. This is called "typecasting".
Type casting can be done independently. This is done using the following construction:
<?php $string = '123'; $numeric = (int) $string;
The $string variable is now a string, and the $numeric variable is already an integer. For now, we will not dwell on this in detail, but it will not be superfluous to know that this is possible.
Let's now go over the types we've already covered. These are numbers (integer and float), strings. In addition to these, there are several more types of data. We will not all consider them now, but we will consider one more simple type - boolean (boolean value). This type can have only two values: true and false. This type is used to check various conditions and in the next lesson we will look at its application in more detail. That's all about types.