PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




Incrementing/Decrementing Operators in PHP


Tutorials »Php »

Topic

What are the Incrementing/Decrementing Operators ?



Explanation

PHP supports pre/post increment and decrement operators. The following table lists all the operators

Example Name Effect
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

Post Increment/Decrement Operators
Example:
    <?php
    $a = 5;
    //Post Increment
    echo "Value of a: " . $a++ ."<br />\n";
    echo "Value of a post incremented: " . $a . "<br />\n";
    //Post Decrement
    $a = 5;
    echo "Value of a: " . $a-- . "<br />\n";
    echo "Value of a post decremented: " . $a . "<br />\n";
    ?>
Result:
    Value of a: 5
    Value of a post incremented: 6
    Value of a: 5
    Value of a post decremented: 4

In the above example, when the post increment or decrement operator is called it returns the same value, after that it increments or decrements the value.

Pre Increment/Decrement Operators
Example:
    <?php
    Pre Increment
    $a = 5;
    echo "Pre incremented value: " .++$a . "<br />\n";
    echo "Value is same: " .$a . "<br />\n";
    Pre Decrement
    $a = 5;
    echo "Pre decremented value: ".--$a ."<br />\n";
    echo "Value is same: ".$a ."<br />\n";
    ?>
Result:
    Pre incremented value: 6
    Value is same: 6
    Pre decremented value: 4
    Value is same: 4

In the above example, when the pre increment or decrement operator is called it returns decremented or incremented value first.








A Note

Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial. Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.

Other Links

web hosting