0

The PHP documentation tells us about the simple uses of the infix and postfix operators, i.e.

  ++$a;
  $a++;
  (...)(++$a)(...);
  (...)($a++)(...);

Are the four above the only allowed forms for using ++ ?

For example, what about ++($x*$y) or ($u*$v)++ ? It seems to me intuitively clear that this ++ should only be used on "simple", "non-anonymous" variables, but I did not see that specified clearly in the documentation. After all, the interpreter may construct temporary, anonymous variables when computing a complex one-lined expression.

Ewan Delanoy
  • 1,276
  • 2
  • 13
  • 31
  • `++$i` for pre and `$i++` for post, so yes, you are correct – Cârnăciov Mar 01 '16 at 09:34
  • Although I feel the need to point out that PHP is never really ***compiled***, it's ***interpreted***, like python or java bytecode – Cârnăciov Mar 01 '16 at 09:35
  • 2
    [PHP Documentation reference](http://www.php.net/manual/en/language.operators.increment.php).... if in doubt, read the PHP Docs – Mark Baker Mar 01 '16 at 09:38
  • 1
    Probably would have made more sense to open a new question, rather than completely edit your question to a new one. – Mikey Mar 01 '16 at 10:13
  • @Pigeon I agree with you actually, but as that flawed question now has answers I cannot delete it. Perhaps I can ask moderators to close it – Ewan Delanoy Mar 01 '16 at 10:14
  • Whether pre or post increment, `++` increments the value that is being operated on, as well as returning that value (pre or post increment).... while the result of an expression can be returned, it cannot be incremented because it doesn't exist in tangible form, only transient form; making the increment part meaningless for anything other than variables – Mark Baker Mar 01 '16 at 10:50

4 Answers4

0

You're right essentially if you put the -- or ++ operators before a variable then it will be a pre-increment, i.e. ++$i. If you put them after the variable then it will be a post increment $i++.

I would suggest a quick read over this answers here, it contains some interesting information in terms of how they work.

Defaulting to ++$i appears to be the better way to go about it and only using $i++ when you have a specific reason to do so.

Community
  • 1
  • 1
Mikey
  • 2,606
  • 1
  • 12
  • 20
0

PostFix

$in=0;
for ($i=0; $i < 5; $i++) { 
    print_r($in++);//PostFix
    print_r('<br/>');
}

Output

0
1
2
3
4

PreFix:

$in=0;
for ($i=0; $i < 5; $i++) { 
    print_r(++$in);//PreFix
    print_r('<br/>');
}

Output

1
2
3
4
5
Ram Pukar
  • 1,583
  • 15
  • 17
0

This Example explains everything regarding POSTFIX and PREFIX.

Operator  Name            Description 
++$x      Pre-increment   Increments $x by one, then returns $x  
$x++      Post-increment  Returns $x, then increments $x by one 
--$x      Pre-decrement   Decrements $x by one, then returns $x  
$x--      Post-decrement  Returns $x, then decrements $x by one

Example:-

<?php
echo "<h3>Postincrement</h3>";
$a = 5;
echo "Should be 5: " . $a++ . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";

echo "<h3>Preincrement</h3>";
$a = 5;
echo "Should be 6: " . ++$a . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";

echo "<h3>Postdecrement</h3>";
$a = 5;
echo "Should be 5: " . $a-- . "<br />\n";
echo "Should be 4: " . $a . "<br />\n";

echo "<h3>Predecrement</h3>";
$a = 5;
echo "Should be 4: " . --$a . "<br />\n";
echo "Should be 4: " . $a . "<br />\n";
?>

Check this link for more detail:-

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

-Arithmetic operators

-Assignment operators

-Comparison operators

-Increment/Decrement operators

-Logical operators

-String operators

-Array operators

Check this link for PHP operators

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
-1

Yes that's correct, you can read everything about that back on php.net

http://php.net/manual/en/language.operators.increment.php

this will cover it

WowThatsLoud
  • 115
  • 7