As the manual says, decrementing NULL in this way yields NULL, although incrementing it yields 1, as you might expect. Can't quite see why this makes sense, but if you need to work around it, you can use '-= 1' instead:
<?php
$i = null;
--$i;
var_dump($i); // NULL
$i--;
var_dump($i); // NULL
$i-=1;
var_dump($i); // int(-1)
?>
Note that -= returns the value assigned, so treat it like '--$i', not '$i--' if you're testing the value.
Inkrement- bzw. Dekrementoperatoren
PHP unterstützt Prä- und Post-Inkrement- und Dekrementoperatoren im Stil der Programmiersprache C.
| Beispiel | Name | Auswirkung |
|---|---|---|
| ++$a | Prä-Inkrement | Erhöht den Wert von $a um eins (inkrementiert $a) und gibt anschließend den neuen Wert von $a zurück. |
| $a++ | Post-Inkrement | Gibt zuerst den aktuellen Wert von $a zurück und erhöht dann den Wert von $a um eins. |
| --$a | Prä-Dekrement | Vermindert den Wert von $a um eins (dekrementiert $a) und gibt anschließend den neuen Wert von $a zurück. |
| $a-- | Post-Dekrement | Gibt zuerst den aktuellen Wert von $a zurück und erniedrigt dann den Wert von $a um eins. |
Ein einfaches Beispiel-Skript:
<?php
echo "<h3>Post-Inkrement</h3>";
$a = 5;
echo "Sollte 5 sein: " . $a++ . "<br />\n";
echo "Sollte 6 sein: " . $a . "<br />\n";
echo "<h3>Pre-Inkrement</h3>";
$a = 5;
echo "Sollte 6 sein: " . ++$a . "<br />\n";
echo "Sollte 6 sein: " . $a . "<br />\n";
echo "<h3>Post-Dekrement</h3>";
$a = 5;
echo "Sollte 5 sein: " . $a-- . "<br />\n";
echo "Sollte 4 sein: " . $a . "<br />\n";
echo "<h3>Pre-Dekrement</h3>";
$a = 5;
echo "Sollte 4 sein: " . --$a . "<br />\n";
echo "Sollte 4 sein: " . $a . "<br />\n";
?>
PHP folgt bei der Behandlung arithmetischer Operationen an Zeichenvariablen der Perl-Konvention und nicht der von C. Zum Beispiel wird in Perl aus 'Z'+1 'AA', während aus 'Z'+1 in C '[' wird ( ord('Z') == 90, ord('[') == 91 ). Beachten Sie, dass Zeichenvariablen zwar inkrementiert aber nicht dekrementiert werden können.
Beispiel #1 Arithmetrische Operationen an Zeichenvariablen
<?php
$i = 'W';
for($n=0; $n<6; $n++)
echo ++$i . "\n";
/*
Erzeugt in etwa folgende Ausgabe:
X
Y
Z
AA
AB
AC
*/
?>
Inkrement- bzw. Dekrementoperatoren
rowan dot collins at gmail dot com
14-Jun-2007 04:34
14-Jun-2007 04:34
Q1712 at online dot ms
21-Apr-2007 03:52
21-Apr-2007 03:52
A more detailed explanation of the string incremant is:
First of all it is checked wether the string is a standart representaion of a number wich is true if it equals the regex /^ *[+-]?[0-9]*(\.[0-9]|[0-9]\.)[0-9]*([eE]?[+-]?[0-9]+)?$/
but not the regex /\+\./ (no idea why).
if it does, the type is changed to integer (if it equals /^ *[+-]?[0-9]+$/) or to float and then incremented by one.
An empty string becomes the string "1".
Otherwise if the last character is one of [0-8], [a-y] or [A-Y] it is incremented. If it is Z it puts it back to A, is z to a, if 9 to 0 and trys to do the same with the previouse character.
If a character is reatched that is not in [0-9a-zA-Z], nothing is done anymore (that's why " Z" will increment to " A").
If the begining is reached a new caracter is prepended. "1" "a" or "A" depending on wether the first character was "9", "z" or "Z".
If the last character was not [0-9a-zA-Z] the string isn't chaged.
hope this helps someone
Are Pedersen
01-Mar-2007 12:08
01-Mar-2007 12:08
Something to think about:
$a=1;
$a += $a++ + ++$a;
echo $a;
will give you 7.
Why is this?
1. ++$a is first incremented. Now $a is 2.
$a += $a++ + 2
$a is 2
2. $a++ is added to 2 then $a is incremented
$a += 2 + 2
$a is 3
3. now the value of 2 + 2 is added to $a ($a is 3)
$a = $a + 2 + 2
Answer: 3 + 2 + 2 = 7
julien-bernie-laurent at polenord.com
01-Mar-2006 04:55
01-Mar-2006 04:55
to thus trying to increment a string and are blocked by the exponential typecast explained in the message below, here is a small function :
function increment($var) {
$var2 = '_'.$var;
return substr(++$var2,1);
}
timo at frenay dot net
25-Aug-2004 05:45
25-Aug-2004 05:45
JMcCarthy AT CitiStreet DOT com:
As for your March 31 post, at least in PHP version 4.3 this no longer holds for 'D'. Your point is still valid for 'e' or 'E' and worth noting.
Your comment from May 12 is simply not true, although it might be a bug in your specific version of PHP but that would seem very strange.
<?php
$Align = array('a', 'b', 'c');
$i = 0;
echo $Align[$i++]; // Prints 'a', as expected
?>
It might be interesting to know that pre-/postincrement assumes a value of 0 for undefined variables, but pre-/postdecrement does not:
<?php
echo var_dump(++$foo); // int(1)
echo var_dump(--$bar); // NULL!
?>
31-Mar-2004 11:19
Note that incrementing strings can give unpredictable results due to type changes. For example:
<?php
$i = '9C6';
for($n=0; $n<10; $n++)
echo ++$i . "\n";
?>
Gives you:
9C7
9C8
9C9
9D0
10
11
12
..etc.
The 'D' (and also 'E') characters are interpreted here as exponents of 10 (i.e., scientific notation) formatted numbers. Using '9D6' will give 9000001, 9000002, etc.
You might want to use all alphabetical or all numerical, but not mix the two otherwise you may not get what you expect..
chris at free-source dot com
07-Feb-2004 01:11
07-Feb-2004 01:11
Interesting performance note:
$i++ seems to be slightly slower than ++$i, when used on a line by itself the 2 have the same purpose. It's not much, but over 100,000 incements the pre-increment is about .004 seconds faster on average.
mu at despammed dot net
15-Oct-2002 06:11
15-Oct-2002 06:11
The exact moment when post-increment and post-decrement happen is _just immediately after the variable is evaluated_ (not "after the line is processed" or something like that)
Example 1:
$i = 2;
echo $i++ + $i;
Result: 5. The first i is evaluated as 2, gets incremented to 3. i is then evaluated as 3 for the second occurance.
Example 2:
$i = 2;
echo $i + $i++;
Result: 4. The first i is 2. Second i is 2 too, gets incremented afterwards.
cleong at letstalk dot com
18-Oct-2001 04:52
18-Oct-2001 04:52
Note that the ++ and -- don't convert a boolean to an int. The following code will loop forever.
function a($start_index) {
for($i = $start_index; $i < 10; $i++) echo "\$i = $i\n";
}
a(false);
This behavior is, of course, very different from that in C. Had me pulling out my hair for a while.
fred at surleau dot com
18-Jul-2001 09:02
18-Jul-2001 09:02
Other samples :
$l="A"; $l++; -> $l="B"
$l="A0"; $l++; -> $l="A1"
$l="A9"; $l++; -> $l="B0"
$l="Z99"; $l++; -> $l="AA00"
$l="5Z9"; $l++; -> $l="6A0"
$l="9Z9"; $l++; -> $l="10A0"
$l="9z9"; $l++; -> $l="10a0"
$l="J85410"; $l++; -> $l="J85411"
$l="J99999"; $l++; -> $l="K00000"
$l="K00000"; $l++; -> $l="K00001"