Re: Parsing variables within string variables

From: Date: Fri, 07 Apr 2006 17:31:15 +0000
Subject: Re: Parsing variables within string variables
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-233453@lists.php.net to get a copy of this message
I wrote:
You need to EVALUATE the string coming from the database: Assuming that $sDataField contains the string 'Hello $foo':
        $foo = "cat";
        $sText = eval($sDataField);
RESULT: $sText = "Hello cat" http://php.net/eval
I was assuming that you meant that the string "Hello $foo" -- including the dollar sign -- came from the database. If $foo exists as a native PHP variable, I'd want to see your actual code to tell why it's not being properly evaluated by the parser. Sometimes you need to use curly braces to help the PHP interpreter differentiate a variable from the surrounding text:
        $bar = "Hello ${foo}amaran";
If you escape the dollar sign, the variable won't be evaluated:
        $bar = "Hello \$foo";
        RESULT: "Hello $foo"
Paul

« previous php.general (#233453) next »