Page 1 of 1

PHP Noob question => operator usage

Posted: Mon Mar 13, 2006 2:00 pm
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!!

Posted: Mon Mar 13, 2006 2:32 pm
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.

Posted: Mon Mar 13, 2006 2:34 pm
by timvw

Posted: Mon Mar 13, 2006 2:35 pm
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!

Posted: Mon Mar 13, 2006 2:36 pm
by TDonaghe
Thank you very much!

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

Thanks again!