FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Doubt regarding an array of references...
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Doubt regarding an array of references... [message #177510 is a reply to message #177509] Tue, 03 April 2012 12:15 Go to previous messageGo to previous message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma:
Senior Member
Am 03.04.2012 14:03, schrieb Jerry Stuckle:
> On 4/3/2012 7:39 AM, M. Strobel wrote:
>>
>> My 2ยข:
>>
>> The problem with the counted for-loop is tedious writing, and off-by-one error.
>>
>
> No error if you remember what you're doing. They can be tedious, but allow more
> control over the loop. The real problem here is the indicies must be integers and
> sequential.
>
> OTOH, foreach() loops process the elements in the order they were added to the array,
> which is not necessarily numeric order, i.e.

Very true, and very apparent when you mix numeric and alphanumeric keys. For me this
is a feature.

>
>> The problem with the foreach-loop is: don't forget to use the key when you want to
>> change values!
>>
>> This works:
>>
>> #--------------------------------
>> strobel@s114-intel:~> php -a
>> Interactive shell
>>
>> php> $a = array(1,2,3);
>> php> foreach ($a as $k=>$v) { $a[$k]++; }
>> php> var_dump($a);
>> array(3) {
>> [0]=>
>> int(2)
>> [1]=>
>> int(3)
>> [2]=>
>> int(4)
>> }
--cut--
>>
>> I see references in PHP as "a can of worms", in PHP5 they are only necessary in very
>> special cases.
>>
>> /Str.
>>
>
> References are very handy - and prevent the need for such gyrations as you're going
> through.
>
> I find this much easier to understand:
>
> $a = array(1,2,3);
> foreach ($a as &$v) {
> $v++;
> }

Yeah, this looks clear to me. Anyway I would advice any beginner to stay away from
references in the first place.

Especially with all this old PHP4 code in the wild you get on the wrong track.

/Str.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: A little tip
Next Topic: How to etablish an SSH2 tunnel with php ?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 17:11:06 GMT 2024

Total time taken to generate the page: 0.03901 seconds