
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Increment/Decrement Operators
Introduction
C style increment and decrement operators represented by ++ and -- respectively are defined in PHP also. As the name suggests, ++ the increment operator increments value of operand variable by 1. The Decrement operator -- decrements the value by 1. Both are unary operators as they need only one operand. These operators (++ or --) can be used in prefix or postfix manner, either as an expression or along with other operators in a more complex expression.
Syntax
$x=5; $x=5; $y=5; $x++; //postfix increment $y--; //postfix decrement ++$y; //prefix increment --$x; //prefix decrement
When used independently, postfix and prefix increment/decrement operator behave similarly. As a result, $x++ and ++$x both increment value of $x by 1. similarly $y-- as well as --$y both decrement valaue of $y by 1
Following code shows effect of increment/decrement operator in post/prefix fashion
Example
<?php $x=5; $y=5; $x++; //postfix increment $y--; //postfix decrement echo "x = $x y = $y" . "
"; ++$y; //prefix increment --$x; //prefix decrement echo "x = $x y = $y" . "
";; ?>
Output
Following result will be displayed
x = 6 y = 4 x = 5 y = 5
When used in assignment expression, postfix ++ or -- operator has lesser precedence than =. Hence $a=$x++ results in $a=$x followed by $x++. On the other hand, prefix ++/-- operators have higher precedence than =. Therefore $b=--$y is evaluated by first doing --$y and then assigning resultant $y to $b
Example
<?php $x=5; $y=5; $a=$x++; //postfix increment echo "a = $a x = $x" . "
"; $b=--$y; //prefix decrement echo "b = $b y = $y" . "
"; ?>
Output
Following result will be displayed
a = 5 x = 6 b = 4 y = 4
Increment/ operation with ASCII character variables is also possible. Incrementation result in next character in the ASCII set. If incrementation exceeds the set, i.e. goes beyond Z, next round of ASCII set is repeated i.e. variable with value Z will be incremented to AA. Non-ASCII characters (other than A-Z, a-z and 0-9) are ignored by increment operator.
Example
<?php $var='A'; for ($i=1; $i<=3; $i++){ echo ++$var . "
"; } $var1=1; for ($i=1; $i<=3; $i++){ echo ++$var1 . "
"; } ?>
Output
Following result will be displayed
B C D 2 3 4
- Related Articles
- Python Increment and Decrement Operators
- Increment and decrement operators in Java
- Increment and Decrement Operators in Python?
- Increment ++ and decrement -- Operators in C++
- Increment and Decrement Operators in C#
- Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?
- What are increment (++) and decrement (--) operators in C#?
- Interesting facts about Increment and Decrement operators in Java
- What are the restrictions on increment and decrement operators in java?
- Pre-increment (or pre-decrement) in C
- Increment ++ and Decrement -- Operator Overloading in C++
- Write a C program to demonstrate post increment and pre increment operators
- Count of suffix increment/decrement operations to construct a given array in C++
- Increment Negative and Decrement Positive Numbers by 1 in an Array in Java
- PHP Array Operators
