ComputersProgramming

Symbols in PHP: string to number and back

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

Up to a certain time, oral speech, hand-made symbols, gestures, and works of art, in particular, were most popular. It is important that all these options of natural representation of information from small to large can always be described in words, that is, made available to software processing in one sense or another.

Strings and numbers

Strings are, first of all, a collection of different symbols. In different programming tools, the fractional part was separated from the whole in different ways. Some preferred a point, other commas (in PHP, the conversion of a string to a number implies "."). The triads were separated by a single apostrophe, comma or space. The real numbers were written in the letter "E".

All this is formal. Mathematics and the principles of operation of any processor have always recognized the number of no-nonsense, and to work with real values, appropriate additional processors have been developed.

But the world of variables of a particular type (for programs of the beginning of the era) and the implementation of the modern consensus idea of all data types means: in PHP to convert a string to a number is like nothing to do. At best, PHP will "cut off", or rather, make a 0 or a number if there is at least one digit in the source string. All digits before the first non-numeric character will make up this number.

The general rule of transformation

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

Any non-numeric character in the sequence of digits stops the process, and the result of the PHP conversion "string to number" is only the value that was generated before the first non-numeric character.

Mathematics and Symbol Processing

What is more complicated, the first or the second, even from the positions of formulas that do not fit on the page, impressive matrices, graphs, which even the authors, derivatives of integrals, differentials from integrals and polynomials in the n-th generation can not unravel even sometimes: society , Undoubtedly honors the achievements of mathematics and mathematics, but the processing of symbols is more important, more complex and covers mathematics, like a duvet, providing an inquisitive mind that can not go beyond the magic of numbers, a favorable environment for creativity.

Usually in PHP, a line in a number is converted automatically, but it will never be superfluous to check it. The best thing to always be sure: PHP correctly understands that a variable is a number. If there is even a shadow of doubt, you should explicitly specify the type of the variable (int) or (float) before the value and check how PHP understands the type of the variable with is_integer () or is_float ().

Trends in modern programming

The classical interpretation of the problem in PHP "string to number" lies within the representations of modern programming. The reverse process has much more diverse options. Functions number_format (), printf (), sprintf () can work wonders, and not only at the level of one number, but also a group of numbers.

атематика и численные вычисления - это не наш конек. Converting to PHP " string to number" indicates, rather, that we can, but mathematics and numerical computations are not ours.

Write a decent and reliable functional that implements this or that processing of numbers in PHP, does not represent problems, and the result will not be anything worse than the result from more mathematical languages. PHP is, first of all, the language of the server, the language for processing information, including mathematical ones.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.