TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Changable value (http://www.talkphp.com/general/5599-changable-value.html)

CΛSTΞX 10-08-2010 09:42 PM

Changable value
 
hey I've little question;

PHP Code:

$get $_REQUEST['NUMBER'];
$num."".$get "blbla";

echo 
$num3

// should echo blbla

I have to echo the changable value name. But its not correct. How do you think I can get it in correct way ?

Village Idiot 10-08-2010 09:53 PM

Quote:

Originally Posted by CΛSTΞX (Post 31081)
hey I've little question;

PHP Code:

$get $_REQUEST['NUMBER'];
$num."".$get "blbla";

echo 
$num3

// should echo blbla

I have to echo the changable value name. But its not correct. How do you think I can get it in correct way ?

What you are doing is impossible. You can not name variables like they are strings. Your closest match would be to use an array.

PHP Code:

$get $_REQUEST['NUMBER'];
$num[$get] = "blbla";

echo 
$num[$get]; 


wGEric 10-08-2010 10:13 PM

Variable variables: http://php.net/manual/en/language.va...s.variable.php

I'm not sure if you can combine two variables like this

PHP Code:

${$num $get

But one variable works

PHP Code:

$var $num $get;
$
$var 


CΛSTΞX 10-08-2010 10:14 PM

Thanks Eric, that is interesting. I wasn't know that.

Salathe 10-09-2010 08:00 AM

See also: http://cowburn.info/2008/01/12/php-vars-curly-braces/

Village Idiot 10-09-2010 05:07 PM

Quote:

Originally Posted by Salathe (Post 31086)

Interesting, I never knew one could do that... I do know that I will never use it though.

delayedinsanity 10-09-2010 08:29 PM

(removed by suggestion)

Salathe 10-10-2010 11:34 PM

Quote:

Originally Posted by Village Idiot (Post 31088)
Interesting, I never knew one could do that... I do know that I will never use it though.

Absolutely the right stance to take.

A rare example of curly-brace-madness being useful comes from lovely web APIs provided by big names (here's looking at you Google and Yahoo!) which provide JSON-formatted responses containing identifiers like foo$bar or foo:bar which when converted directly to stdClass properties (via json_decode) cannot be accessed with the normal syntax (e.g. $object->foo$bar), calling for something like $object->{'foo$bar'} instead.

As for actual variables names, I can't think of the last time that I used the ${...} syntax for production code outside of a double-quoted string; I'm uncertain if it has ever happened.

Quote:

Originally Posted by delayedinsanity (Post 31091)
... I just like coming up with fictional new ways to get back ...

How about you keep this kind of stuff to yourself, next time? Thanks. ^^

delayedinsanity 10-10-2010 11:58 PM

Alrighty, point taken.


All times are GMT. The time now is 07:20 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0