Characters in PHP: string to number and vice versa

Information at all times was a symbol. Only when programming appeared did it partially become letters, numbers and signs (in the formal sense).

Until a certain time, oral speech, hand-made characters, gestures, as well as works of art, in particular, enjoyed the greatest popularity. It is important that all these options for the natural presentation of information, from small to large, could always be described in words, that is, made available to software processing in one sense or another.

Php string to number

Lines and numbers

Strings are, first of all, collections of different characters. In different programming tools, the fractional part was separated from the whole in different ways. Some preferred a period, others a comma (in PHP, converting a string to a number implies a "."). The triads were separated by a single apostrophe, comma or space. Real numbers were written through the letter "E".

All these are formalities. The mathematics and principles of operation of any processor have always recognized numbers without frills, and corresponding additional processors have been developed to work with real values.

PHP string to number conversion

But the world of variables of a specific type (for programs at the beginning of the era) and the implementation of the modern idea of โ€‹โ€‹consensus of all data types means: in PHP, converting a string to a number is like nothing to do. In the best case, PHP will โ€œcut off,โ€ or rather, make 0 or a number if there is at least one digit in the source line. All digits up to the first non-digital character will make up this number.

General Conversion Rule

In PHP, converting a string to a number, unless specified otherwise by the programmer, occurs automatically when applied. If the variable contains only numbers and a period, then a real number can be obtained. If a comma occurs in the sequence of numbers, then everything that comes before it will be interpreted as a number, naturally, an integer.

Any non-digital character in a sequence of numbers stops the process, and the result of converting PHP "string to number" will be only the value that was formed before the first non-digital character.

Mathematics and character processing

What is more difficult, the first or second, even from the point of view of formulas that do not fit on the page, of impressive matrices, graphs, which even authors, sometimes derived from integrals, differentials from integrals and polynomials in the nth generation, can sometimes not unravel: itโ€™s very simple to say: society Undoubtedly honors the merits of mathematicians and mathematicians, but the processing of symbols is more important, more complex and covers mathematics like a duvet, providing an inquisitive mind that cannot go beyond the magic of numbers, a favorable environment for creativity.

PHP convert string to number

Usually in PHP, a string is automatically converted to a number, but it will never be superfluous to check this. Itโ€™s best to always be sure: PHP correctly understands that a variable is a number. If there is even a shadow of doubt, you should explicitly indicate the type of the variable (int) or (float) before the value and check how PHP understands the type of the variable with the functions is_integer () or is_float ().

Modern programming trends

The classic interpretation of a PHP string-to-number task lies within the scope of modern programming. The reverse process has significantly more diverse options. The functions number_format (), printf (), sprintf () can work wonders, and not only at the level of one number, but also a group of numbers.

Converting a string to number in PHP testifies rather that we can, but mathematics and numerical calculations are not our hobby.

And it can, but it is not ours!

Writing a decent and reliable functional that implements this or that number processing in PHP is not a problem, and the result will not be worse than the result from more mathematical languages. PHP is, first of all, the server language, a language for processing information, including mathematical ones.

Source: https://habr.com/ru/post/K11010/


All Articles