Cannot use "-" in variable names?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-09-2005
ross
 
Posts: n/a
Default Cannot use "-" in variable names?

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
Reply With Quote
  #2 (permalink)  
Old 12-09-2005
Malcolm Dew-Jones
 
Posts: n/a
Default Re: Cannot use "-" in variable names?

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
Reply With Quote
  #3 (permalink)  
Old 12-09-2005
Janwillem Borleffs
 
Posts: n/a
Default Re: Cannot use "-" in variable names?

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


Reply With Quote
  #4 (permalink)  
Old 12-10-2005
Kimmo Laine
 
Posts: n/a
Default Re: Cannot use "-" in variable names?

"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>


Reply With Quote
  #5 (permalink)  
Old 12-10-2005
ross
 
Posts: n/a
Default Re: Cannot use "-" in variable names?

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. :)
>
>
>

Reply With Quote
  #6 (permalink)  
Old 12-10-2005
Oli Filth
 
Posts: n/a
Default Re: Cannot use "-" in variable names?

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
Reply With Quote
  #7 (permalink)  
Old 12-10-2005
Stefan Rybacki
 
Posts: n/a
Default Re: Cannot use "-" in variable names?

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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:09 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0