Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 02:10 AM
dan
Guest
 
Posts: n/a
Default Is something like this possible with php?

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


?>



  #2  
Old July 17th, 2005, 02:10 AM
Justin Koivisto
Guest
 
Posts: n/a
Default 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.

  #3  
Old July 17th, 2005, 02:11 AM
kaptain kernel
Guest
 
Posts: n/a
Default 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]

  #4  
Old July 17th, 2005, 02:11 AM
Tom Thackrey
Guest
 
Posts: n/a
Default 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)
  #5  
Old July 17th, 2005, 02:11 AM
Disco Plumber
Guest
 
Posts: n/a
Default 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.
  #6  
Old July 17th, 2005, 02:11 AM
Alvaro G Vicario
Guest
 
Posts: n/a
Default 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
--
  #7  
Old July 17th, 2005, 02:11 AM
Matthew Crouch
Guest
 
Posts: n/a
Default 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]


  #8  
Old July 17th, 2005, 02:11 AM
Pedro Graca
Guest
 
Posts: n/a
Default 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
  #9  
Old July 17th, 2005, 02:11 AM
Disco Plumber
Guest
 
Posts: n/a
Default 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.
  #10  
Old July 17th, 2005, 02:11 AM
Pedro Graca
Guest
 
Posts: n/a
Default 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
  #11  
Old July 17th, 2005, 02:11 AM
Jeffrey Silverman
Guest
 
Posts: n/a
Default 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/

  #12  
Old July 17th, 2005, 02:12 AM
dan
Guest
 
Posts: n/a
Default 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.
  #13  
Old July 17th, 2005, 02:12 AM
dan
Guest
 
Posts: n/a
Default 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
  #14  
Old July 17th, 2005, 02:12 AM
Pedro Graca
Guest
 
Posts: n/a
Default 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
  #15  
Old July 17th, 2005, 02:12 AM
Tom Thackrey
Guest
 
Posts: n/a
Default 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)
 

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.