⊗ppPmBsATC 36 of 447 menu

Automatic Type Conversion in PHP

As you already know, strings in PHP should be enclosed in quotes. It may happen that all characters of a string are digits. In this case, if you perform a mathematical operation on a string with digits - this operation will be performed as if we were dealing with actual numbers, not strings:

<?php echo '1' + '2'; // outputs 3 ?>

In this case, PHP sees that we are trying to perform an operation that is invalid for strings, but valid for numbers. It also sees that, in fact, our strings are numbers in quotes. Therefore, PHP automatically performs the conversion of these strings to numbers and performs the corresponding mathematical operation on them.

Similarly, the addition of a string with digits and a regular number will occur:

<?php echo '1' + 2; // outputs 3 ?>

The order of addition will not matter:

<?php echo 1 + '2'; // outputs 3 ?>

Everything said will work similarly for variables as well:

<?php $a = '1'; $b = '2'; echo $a + $b; // outputs 3 ?>

Without running the code, determine what will be displayed on the screen:

<?php $a = '1'; $b = '2'; echo $a + $b + '3'; ?>
byenru