By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

Is something like this possible with php?

P: n/a
dan
Hi,

I am trying to see if this is possible. I have thought about it and
cant see an easy way to do this.
OK say I have a script

<?php

//these variables get set in the script

$fname='Moe';
$bday='1970';

//extract a field from mysql
//select someField from someTable limit 1;
//the variable someField contains this value

//$someField='Welcome back $fname ! wow you must be an old fart since
you were born in $bday'

As you can see the variable is stored in the mysql table with embedded
variables in the string itself. What I would like to do is have these
variables evaluated somehow so that the $someField value contains this
value

Welcome back Moe ! wow you must be an old fart since you were born in
1970
?>
Jul 17 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
dan wrote:
Hi,

I am trying to see if this is possible. I have thought about it and
cant see an easy way to do this.
OK say I have a script

<?php

//these variables get set in the script

$fname='Moe';
$bday='1970';

//extract a field from mysql
//select someField from someTable limit 1;
//the variable someField contains this value

//$someField='Welcome back $fname ! wow you must be an old fart since
you were born in $bday'

As you can see the variable is stored in the mysql table with embedded
variables in the string itself. What I would like to do is have these
variables evaluated somehow so that the $someField value contains this
value

Welcome back Moe ! wow you must be an old fart since you were born in
1970
?>


Look at the eval() function:
http://us4.php.net/manual/en/function.eval.php

Basically, you'll want to use that on the field's value so that it will
fill in the correct variables.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #2

P: n/a

look up the "eval" command

eval ($someField);

$someField would be:

print "Welcome back $fname ! wow you must be an old fart since
you were born in $bday";
dan wrote: Hi,

I am trying to see if this is possible. I have thought about it and
cant see an easy way to do this.
OK say I have a script

<?php

//these variables get set in the script

$fname='Moe';
$bday='1970';

//extract a field from mysql
//select someField from someTable limit 1;
//the variable someField contains this value

//$someField='Welcome back $fname ! wow you must be an old fart since
you were born in $bday'

As you can see the variable is stored in the mysql table with embedded
variables in the string itself. What I would like to do is have these
variables evaluated somehow so that the $someField value contains this
value

Welcome back Moe ! wow you must be an old fart since you were born in
1970
?>


Jul 17 '05 #3

P: n/a

On 17-Nov-2003, ba*************@yahoo.com (dan) wrote:
$fname='Moe';
$bday='1970';

//extract a field from mysql
//select someField from someTable limit 1;
//the variable someField contains this value

//$someField='Welcome back $fname ! wow you must be an old fart since
you were born in $bday'

As you can see the variable is stored in the mysql table with embedded
variables in the string itself. What I would like to do is have these
variables evaluated somehow so that the $someField value contains this
value


eval('$someField="'.$someField.'";');

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #4

P: n/a
dan (34.235% quality rating):

As you can see the variable is stored in the mysql table with embedded
variables in the string itself. What I would like to do is have these
variables evaluated somehow so that the $someField value contains this


http://us3.php.net/manual/en/function.eval.php

eval is your friend.

/joe
--
Travis Thatcher's case from Bednarz! 3.0 is muddled? In the emo garage,
Housing's desktop is ice-cold.
Jul 17 '05 #5

P: n/a
*** dan wrote/escribió (17 Nov 2003 09:25:12 -0800):
//$someField='Welcome back $fname ! wow you must be an old fart since
you were born in $bday'

As you can see the variable is stored in the mysql table with embedded
variables in the string itself. What I would like to do is have these
variables evaluated somehow so that the $someField value contains this
value


I can think of several methods. The easiest one might be this:

www.php.net/eval
--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #6

P: n/a
Is that a record -- 5 pretty much identical responses?

Matthew "eval me all night long" Crouch

"Alvaro G Vicario" <al******************@telecomputeronline.com> wrote in
message news:x4****************************@40tude.net...
*** dan wrote/escribió (17 Nov 2003 09:25:12 -0800):
//$someField='Welcome back $fname ! wow you must be an old fart since
you were born in $bday'

As you can see the variable is stored in the mysql table with embedded
variables in the string itself. What I would like to do is have these
variables evaluated somehow so that the $someField value contains this
value


I can think of several methods. The easiest one might be this:

www.php.net/eval
--
--
-- Álvaro G. Vicario - Burgos, Spain
--

Jul 17 '05 #7

P: n/a
dan wrote:
Hi,

$fname='Moe';
$bday='1970';


single quotes do not interpolate; double quotes do!

<?php
$fname = 'Moe';
$bday = '1970';
$single_quoted_Field = 'Hi $fname, $bday was long ago.';

$double_quoted_Field = "Hi $fname, $bday was long ago.";

$unquoted_Field = 'Hi ' . $fname . ', ' . $bday . ' was long ago.';

$heredoc_Field = <<<EOF
Hi $fname,
$bday was long ago.
EOF;
echo $single_quoted_Field; // prints Hi $fname, ...
echo $double_quoted_Field; // prints Hi Moe, ...
echo $unquoted_Field; // prints Hi Moe, ...
echo $heredoc_Field; // prints Hi Moe,<newline>...
?>

there is no need to eval().

check
http://www.php.net/manual/en/language.types.string.php

--
..sig
Jul 17 '05 #8

P: n/a
Pedro Graca (53.519% quality rating):

there is no need to eval().


I think you misunderstood the original post. He was not saying he
manually assigned that string in PHP. It was stored in a DB field.

/joe
--
In the College of Computing, the gombizler is optimized. Bednarz! 3.0 is
hoppy. The syphilis is exciting.
Jul 17 '05 #9

P: n/a
Disco Plumber wrote:
Pedro Graca (53.519% quality rating):

there is no need to eval().


I think you misunderstood the original post. He was not saying he
manually assigned that string in PHP. It was stored in a DB field.


Ah! I'm terribly sorry. You're quite right.

Maybe the OP can get the data out of the database with the $fname and
$bday thing already in there?

something like:

$sql = "select concat('Hi ', fname, ', ', bday, ' was long ago.')"
. " from table"
. " where id between 4 and 7";

Of course he'd have to reestructure the database and that may not be a
good idea :)

--
..sig
Jul 17 '05 #10

P: n/a
On Mon, 17 Nov 2003 09:25:12 -0800, dan wrote:
Welcome back Moe ! wow you must be an old fart since you were born in
1970


Hey! I was born in 1970! I'm not *that* old!

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Johns Hopkins University | Baltimore, MD
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #11

P: n/a
dan
ba*************@yahoo.com (dan) wrote in message news:<74**************************@posting.google. com>...
Hi,

I am trying to see if this is possible. I have thought about it and
cant see an easy way to do this.


<SNIP>

Thanks everyone.

I thought it was eval() that would solve it but wasn't sure of the syntax.

It makes sense now.
Jul 17 '05 #12

P: n/a
dan
ba*************@yahoo.com (dan) wrote in message news:<74**************************@posting.google. com>...
Hi,

I have another question regarding this.
Lets say, for example, the text that is being eval()'d contains the
string:
"Welcome to $_SERVER['DOCUMENT_ROOT'] you old fart!"
When I eval() this string I get an error like this.

PHP Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or
`T_NUM_STRING' in /home/...

Is there a way around this?
Thanks
Jul 17 '05 #13

P: n/a
dan wrote:
[...]
"Welcome to $_SERVER['DOCUMENT_ROOT'] you old fart!"
When I eval() this string I get an error like this.

PHP Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or
`T_NUM_STRING' in /home/...

Is there a way around this?


The single quotes are messing up the evaluation.

I tried

---- 8< ---- cut here --------
<?php
$x1 = '$y1 = "Welcome $_SERVER[\'PHP_SELF\'], good-night!";';
$x2 = '$y2 = "Welcome ${_SERVER[\'PHP_SELF\']}, good-night!";';
$x3 = '$y3 = "Welcome $_SERVER[PHP_SELF], good-night!";';

# $x2 ==> add { } around the full variable
# $x3 ==> remove single quotes completely

echo "\n\n-- 1 ------\n";
echo "evaluating [$x1] ...\n";
$z1 = eval($x1);
echo 'y1 = ', $y1, "\n";
echo 'z1 = ', $z1, "\n";

echo "\n\n-- 2 ------\n";
echo "evaluating [$x2] ...\n";
$z2 = eval($x2);
echo 'y2 = ', $y2, "\n";
echo 'z2 = ', $z2, "\n";

echo "\n\n-- 3 ------\n";
echo "evaluating [$x3] ...\n";
$z3 = eval($x3);
echo 'y3 = ', $y3, "\n";
echo 'z3 = ', $z3, "\n";
?>
---- 8< ---- cut here --------

HTH

--
..sig
Jul 17 '05 #14

P: n/a

On 18-Nov-2003, ba*************@yahoo.com (dan) wrote:
I have another question regarding this.
Lets say, for example, the text that is being eval()'d contains the
string:
"Welcome to $_SERVER['DOCUMENT_ROOT'] you old fart!"
When I eval() this string I get an error like this.

PHP Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or
`T_NUM_STRING' in /home/...

Is there a way around this?


Your string has incorrect syntax, it should be:
"Welcome to $_SERVER[DOCUMENT_ROOT] you old fart!"

further eval() does not return a value unless you include a return in the
eval code. You probably want something like:

eval('$str = "Welcome to $_SERVER[DOCUMENT_ROOT] you old fart!";');
echo $str;

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Browse more PHP Questions on Bytes