0

I have a challenge that I have a hard time describing it in a short web search. I'm hoping if I can successfully explain it long form all the experts here can understand what I'm asking and let me know if this is even possible. This is theoretical/academic question, not a troubleshooting question.

Say I have a string in a SQL Table that looks like this:

"Hi my name is $name, very please to meet you!"

In my PHP code I have a variable $name with a value in it. If I manually copy this string into my PHP code, interpolation works as advertised and all is good. But if my script uses mysqli to fetch this string I end up with a string that doesn't interpolate, which I guess sort of makes sense, maybe. My question is, is there a way to force that string to interpolate?

I tried to parse out the variable name, in which case I ended with $varname="$name" and whenever I tried to print $varname I always got $name, not the value of $name. Am I barking up the wrong tree here, is this even possible? Should I be looking for an different ways to accomplish this, like store this string in two parts "Hi my name is " in one table field and ", very please to meet you!" in a different table field and then concatenate all three strings together like:

print "$dbstring1 . $name . $dbstring2";

Naturally this isn't the actual string I'm working with, just trying to keep things simple as this is more of a theoretical/academic question than a troubleshooting question.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
Siggi
  • 175
  • 1
  • 11
  • I would consider using `sprintf()` and storing a template in the db like "Hi my name is %s." Edit to add: this would also let you change the order of the items that are filled in (for instance if you supported multiple languages). – Jerry Jul 05 '22 at 23:41
  • From the duplicate, see [the accepted answer](https://stackoverflow.com/a/49194153/283366) – Phil Jul 05 '22 at 23:51
  • I'm not sure if the proposed "duplicate" answer fully answers this question. If you write a string in double quotes in PHP, it will process those variables. However, if you load an existing string (i.e. from database), it won't. You can use `sprintf()` if you don't need "named" parameters, alternatively have a look at this question: https://stackoverflow.com/questions/5701985/vsprintf-or-sprintf-with-named-arguments-or-simple-template-parsing-in-php – Rylee Jul 06 '22 at 02:05
  • Regardless of which method you use, you will end up writing parameters in a specific format when storing the string, then processing it when retrieving. If using `sprintf`, you would use something like "Hi my name is %s, very please to meet you!". If using an alternate method, it might look like: "Hi my name is :name:, very please to meet you!" – Rylee Jul 06 '22 at 02:07
  • I must have failed at explaining what I was trying to do as these solutions aren't even in the right ball park. – Siggi Jul 09 '22 at 11:34
  • So I totally disagree that this is duplicate of the issue linked These are all just variation on standard interpolation from what I can tell. Array interpolation isn't substantially different from normal. And sprintf doesn't seem much different in that respect either. The core to what I'm trying to accomplish is when $var = "$var2" to have $var resolve to the value of $var2. Asking a new question will just get closed as dup again so I'll just a different solution. – Siggi Jul 09 '22 at 11:40

0 Answers0