Implicit conversion rules for PHP strings and increment/decrement operations on strings that contain letters

Source: Internet
Author: User

The implicit type conversion rules and the increment/decrement operators for strings in PHP have been blurred before, and are summarized today.

One, implicit conversion

Implicit type conversion rules for binary arithmetic operators (http://php.net/manual/zh/language.types.string.php)

Type of first operand Type of second operand Type conversions
Integral type Floating point Type Integer conversion to floating-point type
Integral type String The string is converted to a number, and if the string is converted to a floating-point type, the integral type is also converted to floating-point
Floating point Type String string conversion to floating-point type

A summary is floating point > Integer > String. For example:

Integer + floating point = floating point type

$a = n; $b = 1.5; $c = $a + $b; Var_dump ($c); Float 13.5

 

Integer + (non-numeric and. Starting with) string = integral type

$a = n; $b = ' hello1.5 '; $c = $a + $b; Var_dump ($c); int 12

Here $b converted from a string to an integral type and turned into 0.

Integer + floating-point number beginning with string = floating-point type

$a = n; $b = ' 1.5hello '; $c = $a + $b; Var_dump ($c); Float 13.5

$b automatically converted to float 1.5

Integer + integer number beginning with string = integer

$a = n; $b = ' 1hello '; $c = $a + $b; Var_dump ($c); INT 13

  

Integer + floating-point numeric string = floating-point type

$a = n; $b = ' 1.5 '; $c = $a + $b; Var_dump ($c); Float 13.5

  

Integer + (starting with.) string = floating-point or integral type

$a = n; $b = '. 5hello '; $c = $a + $b; Var_dump ($c); Float 12.5

$b converted into 0.5.

$a = n; $b = '. Hello '; $c = $a + $b; Var_dump ($c); int 12

$b not converted to float 0.0 but converted to int 0

  

Integer + string containing uppercase E or lowercase e (must have mantissa in the format required to satisfy scientific notation) = floating-point type

$a = 1; $b = ' 1e3 '; $c = $a + $b; Var_dump ($c); Float 1001

  

$a = 1; $b = ' 1e-3 '; $c = $a + $b; Var_dump ($c); Float 1.001

  

Integer + hexadecimal digit string = integral type

$a = 1; $b = ' 0x10 '; $c = $a + $b; Var_dump ($c); int 17

  

two, auto increment/decrement operator

Automatic increment of letters

Auto Increment Results
A "B"
"Z" "AA"
"Spaz" "SPBA"
"B9" "C0"
"12" "13"

Example 1: A letter is incremented, and the result is its next letter in the alphabet

$a = ' a '; Var_dump (+ + $a); String ' B ' (length=1)

Example 2:

$a = ' a '; var_dump (--$a); String ' A ' (length=1)

Example 3: "Z" or "Z" is incremented, it becomes "a" or "a", and the character to the left of the character is incremented (if the result of "a" or "a" is at the left of the string the first character, then also need to insert a "a" or "a" on its left)

$a = ' Z '; var_dump (+ + $a); String ' AA ' (length=2)

  

Example 4:

$a = ' Z1 '; Var_dump (+ $a); String ' Z2 ' (length=2)

 

Example 5:

$a = ' Z9 '; Var_dump (+ $a); String ' aa0 ' (length=3)

  

Example 6:

$a = ' B9 '; Var_dump (+ $a); String ' C0 ' (length=2)

  

Example 7:

$a = ' Spaz '; Var_dump (+ $a); String ' SPBA ' (length=4)

  

Example 8:

$a = ' BZ '; Var_dump (+ + $a); String ' Ca ' (length=2)

  

Example 9:

$a = ' 1z '; Var_dump (+ + $a); String ' 2a ' (length=2)

  

Example 10:

$a = ' *a '; Var_dump (+ $a); String ' *b ' (length=2)

  

Example 11:

$a = ' *a* '; Var_dump (+ $a); String ' *a* ' (length=3)

  

Example 12:

$a = ' A * '; Var_dump (+ + $a); String ' A * ' (length=2)

  

  

  

  

  

  

 

Implicit conversion rules for PHP strings and increment/decrement operations on strings that contain letters

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.