PHP string self-calculation and the use of numerical calculation with a detailed explanation

Source: Internet
Author: User
When a string is taken as a numeric value, the result and type are as follows:

If the string does not contain '. ', ' e ' or ' e ' and its numeric value is within the range of the integral type (as defined by Php_int_max), the string will be evaluated as an integer. All other cases are evaluated as float.

The starting part of the string determines its value. If the string starts with a valid numeric value, the value is used. Otherwise its value is 0 (0). The legal value is represented by an optional sign, followed by one or more digits (which may have decimal points), followed by an optional exponential portion. The exponential part is composed of one or more digits followed by ' e ' or ' e '.

The following will be turned into integers:

[+/-] [0-9] [*]

The following will be converted to floating-point numbers:

[+/-] [0-9] [.] [0-9] [e/e] [0-9] [*]
$r = ' 123 ' + 1;var_dump ($r);//output int (124) $r = ' 123.4 ' + 1;var_dump ($r);//output result float (124.4) $r = ' 1.5e4 ' + 1;var_dump ($r )///output result float (15001) $r = ' 123456789123456789 ' + 1;var_dump ($r);//output result float (1.2345678912346E+17) $r = ' 123abc ' + 1;var_d UMP ($R);//output int (124) $r = ' 123e1abc ' + 1;var_dump ($r);//output result float (1231) $r = ' 123.abc ' + 1;var_dump ($r);//output result float (1 $r = ' +123abc ' + 1;var_dump ($r);//output int (124) $r = ' -123abc ' + 1;var_dump ($r);//output int ( -122) $r = ' abc ' + 1;var_dum P ($r);//output result int (1)

If you have determined that you need an integer (floating-point number), you can use Intval () (Floatval ()) to convert the string to an integer (floating point) and then to the other values.

One. Add numeric strings in PHP

1. If there are no valid or numeric strings at the beginning of two strings, the two strings are added as 0;

$a = "abc"; $b = "B01"; $a + $b is 0

2. When the two numeric string is added, a valid number is added.

$a = "n"; $b = "01a"; $a + $b = 101;

3/If the string contains '. ', ' e ', ' e ', the number is treated as a float type.

$a = "n"; $b = "12.6a"; $a + $b = 112.6;
$a = ' ten '; $b = ' 12.3e3 '; $a + $b the same float type float (12310)

Two. Compare two strings in PHP (< > = =);

The two strings in 1.php are compared equally, generally not (= =), because (= =) changes the original data type, but instead uses the strcmp (STR1,STR2) series function strcmp ($str 1, $str 2);( STRCMP is forced to convert to string type for comparison).

$a = ' 123 '; $b = ' 123. ';( A small number of points after $b)

But there is (= =) comparison is the result is true

Var_dump ($a = = $b);

Therefore, it is generally not necessary to compare strings (= =).

In addition, when a number is compared to a string, the string is converted to an int or float type (numberic_string contains '. ', ' e ', ' e ').

' abc ' translates to int (0); ' 12ab ' translates to int (12); ' 12.3e3 ' converted to float (12300);

2. (> <) comparison

2-1: If it is a comparison of two strings, the ASCII code of the corresponding character is compared.

2-2: If a number is compared to a string, the string is first converted to a number and then compared.

2-3: If you are comparing two numeric strings (a purely numeric string), the two strings are first converted to numbers and then compared.

$a = ' n '; $b = ' 20 '; then $a> $b;

But

such as $a= ' 100a; $b = ' 20 '; When the result is $a< $b;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.