[thelist] PHP - Variables in a literal string

Martin McCarthy roland at ninehells.org
Sat Jun 2 00:09:29 CDT 2001


I think this is worthy of being a tip, so...
<tip type="PHP" author="Martin McCarthy">
PHP evaluates single quoted strings as literal, and processes variables in
double quoted string.

$name = "Martin McCarthy";
print('My name is $name'); //This will return "My name is $name"
print("My name is $name"); //This will return "My name is Martin McCarthy"

</tip>
Now you know, and knowing is half the battle.

Martin
----- Original Message -----
From: "Quicksilver" <qsfc at home.com>
To: "Evolt List" <thelist at lists.evolt.org>
Sent: Saturday, June 02, 2001 1:00 AM
Subject: [thelist] PHP - Variables in a literal string


> The following is just an example, I know that if the situation were
similar
> to the example that there would be much better ways of accomplishing the
> task.  The example, however, is for simplicity's sake.
>
> // Ok, say I have a variable:
> $foo = 'hi there';
>
> // And I have a literal string:
> $moo = 'I would like to say $foo';
>
> When echoed, the variable $moo says exactly what I typed... my question is
> this: How can I make it evaluate the $moo string so that when echoed, it
> will display "I would like to say hi there"?
>
> I know there is an eval() function in PHP, and I've read the entry in the
> manual on it, but there must be some concept I'm not grasping or something
> I've overlooked, because I haven't been able to make it work this way.
>
> Thanks as always,
> - Linus
>
>
> ---------------------------------------
> For unsubscribe and other options, including
> the Tip Harvester and archive of TheList go to:
> http://lists.evolt.org Workers of the Web, evolt !
>
>





More information about the thelist mailing list