TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   get variable value out of a string (http://www.talkphp.com/advanced-php-programming/3582-get-variable-value-out-string.html)

pipesportugal 11-09-2008 09:11 PM

get variable value out of a string
 
Hello dear colleagues from the talkphp forum,

I would very much appreciate some help regarding the following subject:

PHP Code:

$variable1 "Hello";
$title '$variable'."1";
echo 
$title

This routine is returning the result:
$variable1

I would like to get the value "Hello" instead.

Can someone explain to me which php function can I use to retrieve the result that I am looking for ?

Thanks in advance,
pipesportugal

Wildhoney 11-09-2008 10:27 PM

No such function needed. The single quotes are printed literally, without printing the contents of a variable, they simply print what you've put. You can either put them all in double quotes, or in no quotes at all:

php Code:
$title = "$variable 1";
$title = $variable . "1";

Salathe 11-09-2008 10:29 PM

There are numerous ways of doing this, here are three:
PHP Code:

$variable1 'Hello';
$title 'variable1';

echo $
$title;
// or
echo ${$title};
// or (assuming global scope)
echo $GLOBALS[$title]; 


pipesportugal 11-09-2008 11:08 PM

Hello Dear friends Wildhoney and Salathe,

Wildhoney, I think that You did not really understand what I meant, most probably by my faulty description of my needs.

Salathe, the $$ thing has solved it and was what I was looking for. I also understood the GLOBALS solution. That would also do the trick.

Thank You to both of You for disposing of Your time.

Regards,
pipesportugal


All times are GMT. The time now is 12:19 PM.

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