Php increment/decrease operator

Source: Internet
Author: User
Example name effect + + $ a add the value of $ a plus one, and then return $. $ A ++ returns $ a after adding, and then adds the value of $. -- Minus $ a before $ a minus one, and then returns $. $ A -- returns $ a after subtraction, and then drops the value of $ a by one. PHP supports the C-style ascending and descending operators.

Note: the increment/decrement operator does not affect Boolean values. Decreasing the NULL value does not work, but the result of increasing the NULL value is 1.

Increment/decrease operator

Example

Name

Effect

+ + $ A add the value of $ a plus one, and then return $.

$ A ++ returns $ a after adding, and then adds the value of $.

-- Minus $ a before $ a minus one, and then returns $.

$ A -- returns $ a after subtraction, and then drops the value of $ a by one.

A simple example script:

 \n";echo "Should be 6: " . $a . "
\n";echo "Preincrement";$a = 5;echo "Should be 6: " . ++$a . "
\n";echo "Should be 6: " . $a . "
\n";echo "Postdecrement";$a = 5;echo "Should be 5: " . $a-- . "
\n";echo "Should be 4: " . $a . "
\n";echo "Predecrement";$a = 5;echo "Should be 4: " . --$a . "
\n";echo "Should be 4: " . $a . "
\n";?>

When processing the arithmetic operation of character variables, PHP follows the Perl habit, not C. For example, in Perl, $ a = 'Z'; $ a ++ changes $ a to 'A', while in C, a = 'Z '; a ++; converts a to '[' (The ASCII value of 'Z' is 90, and the ASCII value of '[' is 91 ). Note that character variables can only increase, cannot decrease, and only letters (a-z and A-Z) are supported ). Otherwise, the variable is invalid and the original string does not change.

Example #1 arithmetic operation involving character variables

 

The above routine will output:

XYZAAABAC

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.