Jump to content

Variables in stored strings


rookie

Recommended Posts

I have a stored string that I want to use for different situations.  The stored string has a variable within it, and looks something like this:

 

Stored string: Keeps current on the latest $department practices. 

 

I also define the variable $department in another table that has job titles.  So ideally, depending upon which job title was chosen by the query, the string might read:

 

"...latest Human Resources best practices."

  or

"...latest Finance best practices." 

 

 

My problem is I can't get the variable $department to display the current value when the string is displayed.  I tried using single and double quotes and curly braces.  Is it even possible to do what I am trying?

 

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/46303-variables-in-stored-strings/
Share on other sites

Wildbug,

Thanks.  I tried it but it didn't work (see below).  I wonder if part of the problem is the string that has the variable is one of a number of strings that gets displayed in a "While.." loop.  Some of the strings will have a variable, some do not.  Here is the line I was using to display the string:

 

echo "<tr><td>eval($behavior)</td></tr>";

 

The stored string is the variable behavior.  There are a number of behaviors.  Within some of the strings will be the variable $department, and some will not have it.

 

Thanks

 

 

 

Thanks.  I tried it but it didn't work (see below). ...

 

echo "<tr><td>eval($behavior)</td></tr>";

 

Of course that won't work... eval is in the string.  Did you read the man page on eval()?  Eval treats a string like a bit of PHP code, then executes it, returning the result.

 

Incidently, there was an error in my code; you need to add a semi-colon within the double quotes at the end of the eval'd string to make it a proper bit of PHP code:

 

$department = 'Finance';
eval("echo 'Keeps current on the latest $department practices.';");  // Note added semi-colon!

 

You could also use sprintf() (this is a link; click on it) or printf() to keep a string with a variable "placeholder" in it.

 

$department = 'Finance';
$string = 'Keeps current on the latest %s practices.';
printf($string,$department);

//  Yields the following:

Keeps current on the latest Finance practices.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.