rookie Posted April 9, 2007 Share Posted April 9, 2007 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 More sharing options...
Wildbug Posted April 9, 2007 Share Posted April 9, 2007 Try eval(). $department = 'Finance'; eval("echo 'Keeps current on the latest $department practices.'"); Link to comment https://forums.phpfreaks.com/topic/46303-variables-in-stored-strings/#findComment-225267 Share on other sites More sharing options...
rookie Posted April 9, 2007 Author Share Posted April 9, 2007 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 Link to comment https://forums.phpfreaks.com/topic/46303-variables-in-stored-strings/#findComment-225326 Share on other sites More sharing options...
boo_lolly Posted April 9, 2007 Share Posted April 9, 2007 you have to give some more code and provide an actual example of what $behavior really contains. ACTUAL VALUE. Link to comment https://forums.phpfreaks.com/topic/46303-variables-in-stored-strings/#findComment-225340 Share on other sites More sharing options...
obsidian Posted April 9, 2007 Share Posted April 9, 2007 echo "<tr><td>eval($behavior)</td></tr>"; You can't use eval() within a string. It is a function to be used in and of itself: <?php eval("echo \"<tr><td>$behavior</td></tr>\";"); ?> Link to comment https://forums.phpfreaks.com/topic/46303-variables-in-stored-strings/#findComment-225345 Share on other sites More sharing options...
Wildbug Posted April 9, 2007 Share Posted April 9, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46303-variables-in-stored-strings/#findComment-225351 Share on other sites More sharing options...
fert Posted April 9, 2007 Share Posted April 9, 2007 $department = "Finance"; $string ="Keeps current on the latest {$department} practices."; echo $string; Simple PHP people Link to comment https://forums.phpfreaks.com/topic/46303-variables-in-stored-strings/#findComment-225367 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.