PHP Noob question => operator usage

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
TDonaghe
Forum Newbie
Posts: 6
Joined: Mon Mar 13, 2006 1:42 pm

PHP Noob question => operator usage

Post by TDonaghe »

Hi,

I've recently started learning PHP. I'm trying to grok the XML stuff and I'm probably getting ahead of myself, but we learn by just trying stuff, right?

Anyways, I'm looking at the article "CLXII. XML Parser Functions." In the final example down towards the bottom, 3. External Entity Example, there's this bit of code:

function startElement($parser, $name, $attribs)
{
echo "<<font color=\"#0000cc\">$name</font>";
if (count($attribs)) {
foreach ($attribs as $k => $v) {
echo " <font color=\"#009900\">$k</font>=\"<font
color=\"#990000\">$v</font>\"";
}
}
echo ">";
}

I'm confused about the foreach($attribs as $k => $v) bit...

I don't know what the => in this context is doing. I understand that ultimately $v contains the value and k$ contains the attribute name because I see that in the result. I know that => in this context has something to do with the $attribs array, but I don't understand that line of code. Can someone explain to me what's going on?

Thanks!!
ody
Forum Contributor
Posts: 147
Joined: Sat Mar 27, 2004 4:42 am
Location: ManchesterUK

Post by ody »

Basicly it breaks up the associative array ($attribs) one pair at a time into a key and value into $k and $v to be handled in the loop, but they can be whatever you want them to be called. If you just done foreach($attribs as $random) you would only get the values in $random, but with $k => $v you get the key AND the value.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

TDonaghe
Forum Newbie
Posts: 6
Joined: Mon Mar 13, 2006 1:42 pm

Post by TDonaghe »

Thanks! I pretty much understand. I'm just frustrated that I can't find documentation on how the => operator works like that. Everything I've seen so far points to => being used just as a way of assigning information into arrays. When I open the php manual (in .chm windows help format) it tells me that it can't search for the string '=>'

Can you point me to information that more fully describes => ?

Thanks!
TDonaghe
Forum Newbie
Posts: 6
Joined: Mon Mar 13, 2006 1:42 pm

Post by TDonaghe »

Thank you very much!

Exactly what I was looking for. Whew. I was just searching for the wrong thing.

Thanks again!
Post Reply