dashes in php variable names

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
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

dashes in php variable names

Post by dannymc1983 »

im trying to create a client program that uses nusoap to get weather information in XML format from a server site. now i have the client accessing the xml but the only problem is is that some of the xml tags have a "-" in them. for example here is a line of the xml:

Code: Select all

<wind-speed type="sustained" units="knots" time-layout="k-p3h-n42-1">
  <name>Wind Speed</name>
  <value>14</value>
notice the tag <wind-speed has a dash in this

to access the wind speed i need to create an array called wind-speed but php doesnt allow for dashes in varliable or array names!!!! anyone know a solution to this problem???
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

You can't do that because a dash is a subtraction operator so it is not a problem. Use an underscore _ instead.
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

yes but i have to create an array called "wind-speed" in order to access the information. the "wind-speed" tag is part of the xml dowloaded from the server and i cant change it.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Please be more specific. Have you already written code that is not behaving as you would like? If you have, please post some of the relevant code here so I can take a look at it.

To be honest with you, I am not really sure why you are so insistent on having dashes so I want to see what can be done and what you have done.
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

have you not read my first post???
im getting xml from a server using a client that i have written. somebody else has written the xml, i cant change it as it resides on the server. for some reason they decided to have one of their tags written as "wind-speed". now to access the wind speed values that are embedded within the tag i have to create an array called wind-speed. its not like i want to put the dash in, its just because the array has to be the same name as the tag "wind-speed". and since someone else wrote that tag i cant change it!!
do you understand now?
I gave an example of the xml in my first post
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Showing me XML is NOT showing me code. Code means PHP when you are on this site. XML is just a fanciful text file.

Just because data in an XML files has a dash does not mean that you need to make a PHP variable representation that is identical. The variable name that you use to name an array need not have anything to do with the name of the data that it represents and in many cases it doesn't.

So that having been said, have you written any PHP at all that access this XML?

BTW - There is no need to be rude. So if you don't want to be respectful and respond appropriately DON'T ASK ANY QUESTIONS.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

doesn't the XML object have accessors for this stuff via arrays? I thought it did..
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

this is a sample of the xml data im trying to access:

Code: Select all

<parameters applicable-location="point1">
  <wind-speed type="sustained" units="knots" time-layout="k-p3h-n42-1">
    <name>Wind Speed</name>
    <value>14</value>
and this is the code i use to access it:

Code: Select all

$xml = simplexml_load_string($result);
foreach($xml->data&#1111;0]->parameters&#1111;0]->wind-speed&#1111;0]->value as $value)
echo "$value,";
but it wont accept the array name wind-speed!
do you know of any other way to get the wind speed values??
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

The user docs on PHP.net have this note. Does this help?
Sometimes tag names do not respect PHP variable name syntax. For example I have a file like this:
<livre>
<�ditorial>
<�dition-originale ref="123">
bla bla bla
</�dition-originale>
</�ditorial>
</livre>
It's a perfectly valid XML file, but
$livre->�ditorial[0]->�dition-originale[0]->attributes()
will not work because of the hyphen.

The only solution I found is to write:
$livre->�ditorial[0]->{"�dition-originale"}[0]->attributes()
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

ok i put in the curly brackets like you suggested and its giving me this error for the line with the curly brackets:
Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference
any ideas?
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

no wait i forgot the quotes!!!
its working now. thanks very much, would have never solved it if you hadnt found that article!!
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

I'm glad I found that article too. I don't have SimpleXML on my server so I couldn't test it. lol
Post Reply