Home > Backend Development > PHP Tutorial > Understanding and precautions for PHP increment and decrement operators

Understanding and precautions for PHP increment and decrement operators

伊谢尔伦
Release: 2023-03-10 22:50:01
Original
3629 people have browsed it

Increment and decrement operator

increment before ++++$a$a increments by 1, then return $a

increment afterward++$a++ returns first $a, then $a increases by 1

before decrementing----$a$a decreases by 1, then returns to $a

and then decrements--$a--first returns $a , then $a decrements by 1

The first note: Increment/decrement operators do not affect Boolean values. Decrementing a NULL value has no effect, but increasing NULL results in 1.

In other words: During increment/decrement operations, the operands will not be converted into integers before operation. 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)
Copy after login

Increment/decrement NULL:

$a = NULL;
var_dump(++$a); // int(1) 
$a = NULL;
var_dump(--$a); // NULL
Copy after login

When dealing with arithmetic operations on character variables, PHP follows the habits of Perl instead of C of.

For example, in Perl

$a = 'Z';
$a++;
Copy after login

will turn $a into 'AA', while in C,

a = 'Z';
a++;
Copy after login

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

Note that character variables can only be incremented, not decremented, and only support pure letters (a-z and A-Z).

For example:

$a="9D9"; 
var_dump(++$a);  // string(3) "9E0"
Copy after login

However, there is another trap here:

$a="9E0"; 
echo ++$a;  // 10
Copy after login

After installing the above rules, 9E1 should be output, but 10 is output here. Why?

If we write it this way, most people will know why. The type of

$a = "9E0"; 
var_dump(++$a);  // float(10)
Copy after login

$a is Floating point, that is to say, 9E0 is the scientific notation of floating point numbers, that is, 9 * 10^0 = 9, incrementing by 9, Of course the result is 10.

Reference:StringConvert to numerical value

Now the question comes again:

$l = "Z99";
$l++;
Copy after login

What is the result? The result is "AA00" according to the rules of the perl language.

There is another note:

Increasing/decrementing other character variables will be invalid, and the original string will not change.

I won’t explain this.

Last note:

$a = '012';
$a++;
var_dump($a);
Copy after login

Is this result '013'? 13? 11?

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

$a = 012;   // 八进制,十进制为 10
$b = "012"; // 转换为整数为十进制 12
Copy after login

What if it starts with 0x?

$a = '0x1A';
$a++;
var_dump($a);   // int(27)
Copy after login

Those starting with 0 are not considered octal, but those starting with 0x are considered hexadecimal.

在 PHP 官方文档中 Integer 整型 还有另一个八进制陷阱:
Copy after login

var_dump(01090); // Octal 010 = Decimal 8

The manual explains this as:

Warning
如果向八进制数传递了一个非法数字(即 8 或 9),则后面其余数字会被忽略。
Copy after login

The above is the detailed content of Understanding and precautions for PHP increment and decrement operators. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template