Why when outputting text, for example, " $value " from the MySQL base, echo displays stupid text " $value ", and not its value expressed in the above code "$ value = '1'"?

After all, in fact, it should write 1 instead of $value ...

  • Welcome to ru.SO! When creating a question, you should give the code corresponding to the minimum example required to reproduce the problem. It is unlikely that someone wants to poke around in the footwoman that you brought. - Dmitriy Simushev
  • I’m not picking one another, I just asked why the text is displayed instead of a variable, and the code left the other way around for greater convenience, so that no one would point out trivial errors and immediately see what was happening. - W_0rld
  • Two minuses hint to you that you have done something wrong. - Dmitriy Simushev
  • Well, let them hint, I personally think that there is nothing bad in a large text, but those who put minuses should print the following format questions: What is the code of mine that doesn’t work? And they post non-working code stubs. - W_0rld
  • So if in mysql is the string "$ value", then the same line will be displayed. Now, if you write $ a = '$ value' and print $ a - again $ string will be displayed. - jekaby

2 answers 2

PHP doesn't owe you anything.

The substitution of variable values ​​into a string ( interpolation ) occurs only if you explicitly define this string in code. If you get a string from somewhere else (database, text file, environment variables, etc.), then no interpolation occurs. The string remains as it is.

What to do with it?

You can try to interpret the string as PHP code, but this is a very bad idea. Here is an example:

 // $template содержит строку 'echo $value;' eval($template); 

The only problem is that the $template line must contain valid PHP code. Well, this is a very big potential vulnerability.

You can try to use regular expressions to replace variables with their values, which is also rather sad.

More correctly, it would be to use some placeholders for variables and dynamically generate a result based on the context. In general terms, it might look something like this:

 $template = '{{value}}'; $context = ['value' => 1]; // Выведет "1". echo render($template, $context); 

In the example above, the render function is an abstract line processor. And yes, this is how template templates work.

    From the question is not clear. Here is a simple example of output.

     $value = 1; echo '$value'; // выдаст $value т.к воспринимается не как переменная а как символы echo "$value"; // выдаст 1 т.к это подобие кантотанации echo $value; // выдаст 1 просто вывод переменной 

    This is from what I understood. If the error is that you have in the database the variable name as $ value then this is a string and there is nothing understandable here.

    Here you can find more details .

    Respect the time of people who want to help you solve your problem follow the rules