Well, I didn’t know. But I figured it out while working with SimpleXML to parse an XML document where the node name had a hyphen in it. I was finding a way to refer that element as a SimpleXMLElement and found a nifty note in PHP Manual. It says if you have unsupported (unsupported in PHP Lexicon for a variable name) characters inside the name of a node, you can access it using a special pattern {’name’}.
So I tried the following code and it show that you can have a class variable named “i can have cheese burger”
$s = new stdClass();
$s->{’i can have cheese burger’} = “Oh Yeaaah”;
echo $s->{’i can have cheese burger’}; //it will output “Oh Yeeah”
and if you print_f the object $s, you will see the following
stdClass Object
(
[i can have cheese burger] => Oh Yeeah
)
Well, Its funny! - I love having some fun writing PHP code!














14 responses so far ↓
Josh Johnston // October 20, 2007 at 8:59 am
That’s funny that you found this today as I stumbled across the same thing today by accident.
I was running a PostgreSQL query that aliased fields with meaningful names:
SELECT name AS “User Name”, …..
and did a var_dump() of pg_fetch_object() and noticed that the member variable had a space in it. Had no idea how to access it so I used a fetch assoc instead.
junal // October 20, 2007 at 10:23 am
$s = new stdClass();
$s->{”is it funny???”} = “Oh Yeaaah!!”;
echo $s->{”is it funny???”};
andrisp // October 20, 2007 at 12:22 pm
${’i can have cheese burger’} = “Oh Yeeah”;
echo ${’i can have cheese burger’};
Andra P žurnāls » Blog Archive » Kārtējais interesantais PHP mainīgo pierakstu veids // October 20, 2007 at 12:51 pm
[...] rakstīju par PHP mainīgo neierastu izmantošanu (pievērsiet uzmanību arī komentāriem). Tikko uzzināju, ka ir vēl viens interesants veids kā definēt PHP [...]
Rubel // October 21, 2007 at 11:26 am
Ya, This is funny. It also prove php is a Flexible language.

Ehab // October 24, 2007 at 11:14 am
AAAH
I can haas Cheese Burger After All !
TRIVUz // October 24, 2007 at 2:02 pm
(maring kating hoilo :P)
EllisGL // October 24, 2007 at 8:26 pm
Oh no! While it might be neat to be able to do this.. can you image somone that like to have super long descriptive variables using this!?!?!??!?
developercast.com » Hasin Hayder’s Blog: Did you know ‘you can have cheese burger’ could be a variable name? // October 24, 2007 at 9:01 pm
[...] Hayder has a quick tip for users of SimpleXML - a handy way to access a node in an XML document that has unsupported [...]
Did you know ‘you can have cheese burger’ could be a variable name? | MT-Soft Website Development // October 25, 2007 at 8:30 am
[...] could be a variable name? Posted in October 24th, 2007 by admin in Programming Hasin Hayder has a quick tip for users of SimpleXML - a handy way to access a node in an XML document that has unsupported [...]
developercast.com » ProPHP Podcast: Newscast for October 26, 2007 // October 26, 2007 at 11:13 pm
[...] Weird variable names [...]
Arafat Rahman // October 30, 2007 at 12:31 pm
Really funny.
Did you know “you can have cheese burger” could be a variable name? | Online Sid // November 23, 2007 at 1:10 am
[...] Did ya? [...]
Istiak Shams // December 8, 2007 at 6:14 pm
That was funny…
Leave a Comment