If you like SEOmastering Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Incrementing operators

Started by chinmay.sahoo, 12-21-2015, 04:32:57

Previous topic - Next topic

chinmay.sahooTopic starter

PHP inherits a lot of its syntax from C, and C programmers are famously proud of their own conciseness. The incrementing/decrementing operators taken from C make it possible to more concisely represent statements like $count = $count + 1, which tend to be typed frequently.

The increment operator (++) adds one to the variable it is attached to, and the decrement operator (--) subtracts one from the variable. Each one comes in two flavors, postincrement (which is placed immediately after the affected variable), and preincrement (which comes immediately before). Both flavors have the same side effect of changing the variable's value, but they have different values as expressions. The postincrement operator acts as if it changes the variable's value after the expression's value is returned, whereas the preincrement operator acts as though it makes the change first and then returns the variable's new value. You can see the difference by using the operators in assignment statements, like this:

Quote$count = 0;
$result = $count++;
print("Post ++: count is $count, result is $result<BR>");
$count = 0;
$result = ++$count;
print("Pre ++: count is $count, result is $result<BR>");
$count = 0;
$result = $count--;
print("Post --: count is $count, result is $result<BR>");
$count = 0;
$result = --$count;
print("Pre --: count is $count, result is $result<BR>");

which gives the browser output:

QuotePost ++: count is 1, result is 0
Pre ++: count is 1, result is 1
Post --: count is -1, result is 0
Pre --: count is -1, result is –1

In this example, the statement $result = $count++; is exactly equivalent to

Quote$result = $count;
$count = $count + 1;

while $result = ++$count; is equivalent to

Quote$count = $count + 1;
$result = $count;



If you like SEOmastering Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...