PHP operators

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jimbo9
Forum Newbie
Posts: 2
Joined: Thu Jun 09, 2005 6:35 am
Location: UK

PHP operators

Post by jimbo9 »

I have just started to lean PHP and my manual "PHP and MySQL Development" by Welling & Thomson shows the following code re a multidimensional array:

Code: Select all

$products = array( array( 'Code' => 'Tire',
                          'Description' => 'Tires',
                          'Price' =>100
                         ),
etc...

There is absolutely NOTHING I can find that explains exactly what the operator "=>" does. Can anyone please tell me as it keeps cropping up in oscommerce too!

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/size]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Its so you can add a key to a value.

Code: Select all

$products = array('chair','stamp','nut');

echo $product[0]; //chair
echo $product[1]; //stamp
would now be with a key

Code: Select all

$products = array('chair' => 'brown','stamp' => 'red','nut' => 'purple');

echo $product['chair']; //brown
echo $product['stamp']; //red
This way you can now handle things a lot easier. Especially in loops

Code: Select all

$products = array('chair' => 'brown','stamp' => 'red','nut' => 'purple');

foreach ($products as $key => $value) {
     echo $key .' - '. $value .'<br />'; 
}
jimbo9
Forum Newbie
Posts: 2
Joined: Thu Jun 09, 2005 6:35 am
Location: UK

Post by jimbo9 »

Many thanks for swift reply. And I will use the PHP formatting next time!
Post Reply