LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-08-2003, 01:49 PM   #1
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
PHP eval() function


<?php
$test = 'success';

//Case 1. simple variable
$string = 'This is a $test';

// Case 2. Indexed Array variable
$string = 'This is a $_SERVER["SERVER_NAME"]';

echo $string . "<br>";
eval("\$string = \"$string\";");
echo $string . "<br>";
?>

Case 1. Works fine.
Case 2. Errors out at the eval() function. Yes, I've tried escaping the quotes, doubling them and what not!!

I dont want to use string concatenation to make this work.
 
Old 01-09-2003, 01:12 PM   #2
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 17
It seems to work like this:

Code:
<?php
$test = 'success';

//Case 1. simple variable
$string = 'This is a $test';

// Case 2. Indexed Array variable
$string = 'This is a {$_SERVER["SERVER_NAME"]} ';

echo $string . "<br>";
eval("\$string = \"$string\";");
echo $string . "<br>";
?>
here's a little snippet from the php site which
explains how to do variable substitution in quoted
strings:

,----[ http://www.php.net/manual/en/language.types.string.php ]
| Complex (curly) syntax
|
| This isn't called complex because the syntax is
| complex, but because you can include complex
| expressions this way.
|
| In fact, you can include any value that is in the
| namespace in strings with this syntax. You simply
| write the expression the same way as you would outside
| the string, and then include it in { and }. Since you
| can't escape '{', this syntax will only be recognised
| when the $ is immediately following the {. (Use "{\$"
| or "\{$" to get a literal "{$"). Some examples to make
| it clear:
`----


I hope this helps.

<BroadHint>
If it does, please notice that I have an Affero button.
</BroadHint>
 
Old 01-09-2003, 02:39 PM   #3
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Original Poster
Rep: Reputation: 30
Geez, that is EXACTLY what I was looking for. Kicks butt!!

Thanks a ton, leed25. I was about to conclude that the PHP parser wont be able to handle such a construct.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP !!! php mail function akamaru607 Programming 22 08-26-2005 02:44 PM
php HTTP_RAW_POST_DATA no value in function dinges Programming 1 12-27-2004 11:59 AM
need help in PHP header() function ArulPrabhuT Programming 1 11-26-2004 10:17 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM
Using the PHP mail() Function !?!? Hdata Programming 0 06-22-2003 06:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration