phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP Operators: Increment or Decrement Operator

Tag: operator, Increment operator, Decrement Operator   Category: PHP Basic
post: 08 Mar 2008 read: 577


PHP Operators Step By Step Tutorial - Part 10: Increment/Decrement Operator is useful for addition and reduction of principal value by applying pre- and porst principe.

The table of Increment/Decrement Operator:

Operator Info Example
++ Addition Value of b will increase before (pre-) expression $ a ++ $ b is done.
1. $b = 5;
$a = ++ $b;
hence its result :
$a = 6
$b = 6
value of b will increase after (post-) expression $ a = $ b ++ is done.
2. $b = 5;
$a = $b++;
hence its result :
$a = 5
$b = 6
-- Reduction value of b will decrease before ( pre-) expression $ a -- $ b is done
1. $b = 5;
$a = -- $b;
hence its result :
$a = 5;
$b = 5;
Value of b will increase after (post-) expression $ a = $ b -- is done.
$b = 5; $a = $b--; hence its result :
$a = 5; $b = 4;

Pay attention to the example of using operator increment and decrement at script as following:

File name: incdec.php


<html>
<head>
<title>Using Increment/Decrement Operator</title>
</head>

<body>
<h1>Increment/Decrement Operator</h1>
<?
echo ("<h3>Increment Operator (pre-)</h3>");
$a = 7;
printf ("\$a = %d <br>", $a);
$b = ++$a;
printf ("\$a = %d <br>", $a);
printf ("\$b = %d <br>", $b);
echo ("<h3>Increment Operator (post-)</h3>");
$x = 7;
printf ("\$x = %d <br>", $x);
$y = $x++;
printf ("\$x = %d <br>", $x);
printf ("\$y = %d <br>", $y);
echo ("<h3>Decrement Operator(pre-)</h3>");
$a = 7;
printf ("\$a = %d <br>", $a);
$b = --$a;
printf ("\$a = %d <br>", $a);
printf ("\$b = %d <br>", $b);
echo ("<h3>Decrement Operator (post-)</h3>");
$x = 7;
printf ("\$x = %d <br>", $x);
$y = $x--;
printf ("\$x = %d <br>", $x);
printf ("\$y = %d <br>", $y);
?>
</body>
</html>

The result of incdec.php:

increment or decrement operator


Series this article:
PHP Operators: Introduction
PHP Operators: Arithmetic Operators
PHP Operators: Assignment Operators
PHP Operators: Assignment Operators Table
PHP Operators: Bitwise Operators
PHP Operators: Relation Operators
PHP Operators: Ternary Operator
PHP Operators: Error Control Operator
PHP Operators: Execution Operator
PHP Operators: Increment or Decrement Operator
php Operators: Logic Operator
PHP Operators: String Operator
PHP Operators: Array Operator
PHP Operators: Object Operator
PHP Operators: Operator Priority
PHP Constants: Introduction

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting