PHP Increment Decrement operator understanding and considerations

Source: Internet
Author: User
Increment decrement operator

Before increment ++++ $a $ A increments by 1 and then returns $ A

After increment + + + $a first return $ A, then $ A since 1

Before decrement----$a $ A self minus 1 and then return $ A

After decrement--$a--returns $ A, then $ A minus 1

The first thing to note: The increment/decrement operator does not affect the Boolean value. Decreasing the null value also has no effect, but the result of incrementing null is 1.

In other words: in an increment/decrement operation, the operand is not converted to an integer before it is calculated. If the operand is a Boolean value, the result is returned directly.

Increment/Decrement Boolean value:

$a = true;var_dump (+ + $a); BOOL (true) $a = true;var_dump (--$a); BOOL (true) $b = False;var_dump (+ + $b); BOOL (false) $b = False;var_dump (--$b); BOOL (FALSE)

Increment/decrement null:

$a = null;var_dump (+ + $a); Int (1) $a = null;var_dump (--$a); Null

When dealing with arithmetic operations of character variables, PHP inherits the Perl habit, not the C.

For example, in Perl

$a = ' Z '; $a + +;

Will change $ A to ' AA ', and in C,

A = ' Z '; a++;

Will turn a into ' [' (the ASCII value of ' Z ' is 90, ' [' ASCII value is 91).

Note that character variables can only be incremented, cannot be decremented, and only plain letters (A-Z and A-Z) are supported.

For example:

$a = "9d9"; Var_dump (+ + $a);  String (3) "9E0"

But here's another trap:

$a = "9E0"; echo + + $a;  10

Install the above rules, you should output 9E1, but this is the output of 10. Why?

If we write like this, the big people will know why.

$a = "9E0"; Var_dump (+ + $a);  Float (10)

The type of $a is floating point, that is, 9E0 is the scientific notation for floating-point numbers, that is, 9 * 10^0 = 9, and 9 self-increment, the result is of course 10.

Reference: string conversion to numeric value

And now the question is:

$l = "Z99"; $l + +;

How much is the result? The result is "AA00" according to the Perl language rules.

There is one more important note:

Increment/Decrement Other character variables are not valid and the original string does not change.

That's not going to explain.

One final note:

$a = ' 012 '; $a ++;var_dump ($a);

The result is ' 013 '? 13? 11?

The result of this paragraph is int (13), and the string ' 012 ' is not treated as an octal.

$a = 012;   Octal, decimal is 10$b = "012"; Convert to Integer to decimal 12

What if it starts with 0x?

$a = ' 0x1A '; $a ++;var_dump ($a);   Int (27)

0 is not considered to be octal, but the beginning of 0x is considered hexadecimal.

In the official PHP document, there is another octal trap for integer integers:

Var_dump (01090); Octal 010 = Decimal 8

This is explained in the manual as:

Warning if an illegal number (that is, 8 or 9) is passed to the octal number, the remaining digits are ignored.

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.