Connecting Tech Pros Worldwide Forums | Help | Site Map

Is something like this possible with php?

dan
Guest
 
Posts: n/a
#1: Jul 17 '05
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


?>

Justin Koivisto
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Is something like this possible with php?


dan wrote:
[color=blue]
> 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
>
>
> ?>[/color]

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 - spam@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

kaptain kernel
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Is something like this possible with php?



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";


[color=blue]
>dan wrote:[/color]
[color=blue]
> 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
>
>
> ?>[/color]

Tom Thackrey
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Is something like this possible with php?



On 17-Nov-2003, basement_addict@yahoo.com (dan) wrote:
[color=blue]
> $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[/color]

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

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to jamesbutler@willglen.net (it's reserved for spammers)
Disco Plumber
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Is something like this possible with php?


dan (34.235% quality rating):[color=blue]
>
> 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[/color]

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.
Alvaro G Vicario
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Is something like this possible with php?


*** dan wrote/escribió (17 Nov 2003 09:25:12 -0800):[color=blue]
> //$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[/color]

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

www.php.net/eval


--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Matthew Crouch
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Is something like this possible with php?


Is that a record -- 5 pretty much identical responses?

Matthew "eval me all night long" Crouch

"Alvaro G Vicario" <alvaro_QUITAR_REMOVE@telecomputeronline.com> wrote in
message news:x46xsioayhmo.1lqz4hk4ct2oi.dlg@40tude.net...[color=blue]
> *** dan wrote/escribió (17 Nov 2003 09:25:12 -0800):[color=green]
> > //$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[/color]
>
> I can think of several methods. The easiest one might be this:
>
> www.php.net/eval
>
>
> --
> --
> -- Álvaro G. Vicario - Burgos, Spain
> --[/color]


Pedro Graca
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Is something like this possible with php?


dan wrote:[color=blue]
> Hi,
>
> $fname='Moe';
> $bday='1970';[/color]

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
Disco Plumber
Guest
 
Posts: n/a
#9: Jul 17 '05

re: Is something like this possible with php?


Pedro Graca (53.519% quality rating):[color=blue]
>
> there is no need to eval().[/color]

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.
Pedro Graca
Guest
 
Posts: n/a
#10: Jul 17 '05

re: Is something like this possible with php?


Disco Plumber wrote:[color=blue]
> Pedro Graca (53.519% quality rating):[color=green]
>>
>> there is no need to eval().[/color]
>
> 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.[/color]

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
Jeffrey Silverman
Guest
 
Posts: n/a
#11: Jul 17 '05

re: Is something like this possible with php?


On Mon, 17 Nov 2003 09:25:12 -0800, dan wrote:
[color=blue]
> Welcome back Moe ! wow you must be an old fart since you were born in
> 1970[/color]

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/

dan
Guest
 
Posts: n/a
#12: Jul 17 '05

re: Is something like this possible with php?


basement_addict@yahoo.com (dan) wrote in message news:<744d583c.0311170925.166da7e1@posting.google. com>...[color=blue]
> Hi,
>
> I am trying to see if this is possible. I have thought about it and
> cant see an easy way to do this.[/color]

<SNIP>



Thanks everyone.

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

It makes sense now.
dan
Guest
 
Posts: n/a
#13: Jul 17 '05

re: Is something like this possible with php?


basement_addict@yahoo.com (dan) wrote in message news:<744d583c.0311170925.166da7e1@posting.google. com>...[color=blue]
> Hi,
>[/color]


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
Pedro Graca
Guest
 
Posts: n/a
#14: Jul 17 '05

re: Is something like this possible with php?


dan wrote:[color=blue]
> [...]
> "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?[/color]

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
Tom Thackrey
Guest
 
Posts: n/a
#15: Jul 17 '05

re: Is something like this possible with php?



On 18-Nov-2003, basement_addict@yahoo.com (dan) wrote:
[color=blue]
> 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?[/color]

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 jamesbutler@willglen.net (it's reserved for spammers)
Closed Thread