You are here: Re: $i = $i++ « PHP Programming Language « IT news, forums, messages
Re: $i = $i++

Posted by Rami Elomaa on 04/12/07 14:49

Steve kirjoitti:
> "Steve" <no.one@example.com> wrote in message
> news:JgcTh.1016$AE5.731@newsfe06.lga...
> | someone asked a question in alt.php about a problem they were having with
> an
> | algorytm. it contained something to the effect of:
> |
> | $i = $i++;
> |
> | some example code they'd snatched somewhere. in a loop, the expected $i to
> | increment. i explained why i thought it would not - as it does not.
> however,
> | i want to make sure i gave a valid answer.
> |
> | anyone have input?
>
>
> thanks all. that's how i explained it. i didn't quite get why the value of
> $i remained the same (whatever it's initial value was)...until:
>
> $i is incremented. Then $i is set with the old (pre-increment) value of
> $i (++ has precedence over =).
>
> thanks jerry. it just snapped for me. it's that even though $i++ may
> increment, the lhs assignment is made from $i's initial value prior to the
> increment... it's the = that resets/nullifies the ++ in this case. that
> about right?

Yes. That's why the assignment is not used with $i++. $i++ is a
shorthand that both increments and assigns at the same time. If you go
and add another assignment then it's fucked.

$i = ++$i; // this would've worked as expected, though.

Maybe this helps:

<?php

function ipp(&$i){ // equivalent of $i++
$old_i = $i; // old value is stored in temporary location
$i=$i+1; // value is altered
return $old_i; // old value is returned
}

$i = 5;
$i = ipp($i);
echo $i;

function ppi(&$i){ // equivalent of ++$i
$i=$i+1; // value is altered
return $i; // new value is returned
}

$j = 10;
$j = ppi($j);
echo $j;

?>

--
Rami.Elomaa@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация