⊗ppPmBsNTS 38 of 447 menu

Nuances of String Conversion in PHP

In the previous lesson, we learned how to add numbers as strings. However, there are nuances to such addition when not variables are added, but numbers directly.

The point is that the dot is used not only for string concatenation, but also for separating the fractional part from the integer part.

If you put spaces around the dot, there will be no problems:

<?php echo 1 . 2; // will output '12' ?>

And if you remove the spaces around the dot, we get a float number, not string concatenation:

<?php echo 1.2; // will output 1.2 ?>

Because of such nuances, it is easy to get an error if you put a space on one side of the dot and not on the other:

<?php echo 1. 2; // will throw an error ?>

However, with variables, this problem will not occur:

<?php $a = 1; $b = 2; echo $a.$b; // will output '12', not an error ?>

The code author wanted to add numbers 3 and 4 as strings:

<?php echo 3.4; ?>

However, the code does not work correctly. Fix the author's code error.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline