LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2006, 04:14 PM   #1
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Rep: Reputation: 15
Smile PHP Coding Question


I've been writing some code in php and recently used:

a foreach

but I can't understand what the advantage of using:

foreach (array_expression as $key => $value)
statement

I understand that the array position is set to $key variable as it passes through the array. Is this the only advantage? Also what is the => operator called and can it be used elsewhere?

instead of just:

foreach (array_expression as $value)
statement


Can anyone help me out?

Thanks.

Last edited by joelhop; 07-20-2006 at 04:17 PM.
 
Old 07-20-2006, 05:33 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
the advantage of using a foreach with the $key and $value is that you can access the key. This is specially necesaria if your array is actualy a dictionary (or hash, associatve array or name as you like) and so the keys are important and are not secuential numbers.
The => is just part of the structure of the foreach and it just means that the left part is the key variable and the right part is the value variable. There is no more magic than that

Hope this is useful
Cheers!
 
Old 07-20-2006, 07:16 PM   #3
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
Smile Thanks!

That definitely makes sense.

Thank You!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP 4.4.0 Coding Error Phaux Linux - Software 1 12-30-2005 08:05 PM
Coding a redirect function in php mrobertson Programming 2 06-22-2005 09:40 AM
coding a php server mrobertson Linux - General 22 06-05-2005 02:06 PM
php coding with knoppix mrobertson Linux - General 1 06-01-2005 02:30 PM
vim php color coding? Longinus Linux - Software 4 04-11-2004 06:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration