This is a discussion on Cannot use "-" in variable names? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; The following gives an error: <?php $super-man=100; ?> Parse error: parse error, unexpected '=' in /test.php on ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
The following gives an error:
<?php $super-man=100; ?> Parse error: parse error, unexpected '=' in /test.php on line 2 I understand that the "-" sign is seen as an operator. is there a way to get around this? I need to have "-" in variable names. Thanks, Ross |
|
|||
|
Janwillem Borleffs (jw@jwscripts.com) wrote:
: ross wrote: : > I understand that the "-" sign is seen as an operator. : > is there a way to get around this? I need to have "-" in variable : > names. : Why a hyphen and not an underscore? Anyways if you insist: : ${'super-man'} = 100; : print ${'super-man'}; Technically correct, and good to know for understanding php. However the original poster should please notice that either of the following two would normally be much better in a program $super_heroes['super-man'] = 100; OR $super_man = 100; The best style would depend on the circumstances. In a game of "superman vs kryptonite" the latter would likely be appropriate. In a game of "how many super heroes do your friends know" then the former would likely be best. $0.10 |
|
|||
|
ross wrote:
> I understand that the "-" sign is seen as an operator. > is there a way to get around this? I need to have "-" in variable > names. Why a hyphen and not an underscore? Anyways if you insist: ${'super-man'} = 100; print ${'super-man'}; JW |
|
|||
|
"ross" <aoxo_oxoa@yahoo.com> kirjoitti
viestissä:dncl4u$hl4$1@mailhub227.itcs.purdue.edu. .. > I understand that the "-" sign is seen as an operator. > is there a way to get around this? I need to have "-" in variable names. I'd very much like to hear why you _need_ to use such a poor naming practise. I'd understund if you _want_ to use such, but not that you really really absolutely _need_ to use such. And my apologies, I'm just a really curious person. :) -- SETI @ Home - Donate your cpu's idle time to science. Further reading at <http://setiweb.ssl.berkeley.edu/> Kimmo Laine <antaatulla.sikanautaa@gmail.com.NOSPAM.invalid> |
|
|||
|
I am parsing a xml file and would like to create variables on the fly
with the same name as the XML tags. some of the xml tags are in the form "xx-yy" etc. Kimmo Laine wrote: > "ross" <aoxo_oxoa@yahoo.com> kirjoitti > viestissä:dncl4u$hl4$1@mailhub227.itcs.purdue.edu. .. > >>I understand that the "-" sign is seen as an operator. >>is there a way to get around this? I need to have "-" in variable names. > > > I'd very much like to hear why you _need_ to use such a poor naming > practise. I'd understund if you _want_ to use such, but not that you really > really absolutely _need_ to use such. And my apologies, I'm just a really > curious person. :) > > > |
|
|||
|
ross said the following on 10/12/2005 14:31:
> Kimmo Laine wrote: > >> "ross" <aoxo_oxoa@yahoo.com> kirjoitti >> viestissä:dncl4u$hl4$1@mailhub227.itcs.purdue.edu. .. >> >>> I understand that the "-" sign is seen as an operator. >>> is there a way to get around this? I need to have "-" in variable names. >> >> >> >> I'd very much like to hear why you _need_ to use such a poor naming >> practise. I'd understund if you _want_ to use such, but not that you >> really really absolutely _need_ to use such. And my apologies, I'm >> just a really curious person. :) >> >> > I am parsing a xml file and would like to create variables on the fly > with the same name as the XML tags. some of the xml tags are in the > form "xx-yy" etc. Well firstly, that's dangerous. If someone created an XML document that (accidentally or maliciously) contained elements with the same name as existing PHP variables, you'd be screwed. Secondly, what's the advantage in creating the variables on the fly? Presumably you know the intended structure of the incoming the XML document, otherwise there'd be no point in processing it. Therefore, what's wrong with explicitly assigning values to variables as you parse them? That way, you can avoid the problem I mentioned above. -- Oli |
|
|||
|
ross wrote:
> I am parsing a xml file and would like to create variables on the fly > with the same name as the XML tags. some of the xml tags are in the form > "xx-yy" etc. > Then use the array approach already presented by another poster. $xml_vars['xx-yy']='foobar'; Regards Stefan |
![]() |
| Thread Tools | |
| Display Modes | |
|
|