Replacement of variable in string

Source: Internet
Author: User
The variable replacement method in the string. I want to replace the variable in the string, for example:
$ A = "12345 ";
Echo "a = $ a"; a = 12345 is printed;
This is the result I want. But now there is a problem, that is, I put the string a = $ a in the database and then extract it. what the same statement prints out is
A = $ a. What should I do if I still want to achieve the above effect?


Reply to discussion (solution)

I checked it and said that php only processes parsing in double quotation mark strings. that is to say, what I get from the database is not necessarily treated as a string, right?

"A =". $ a; so?

"A =". $ a; so?



The key is that $ a is treated as "$ a" instead of a php variable.

Why do we need to store variables in the database? your example shows that the value of Directly storing the field is 12345, and echo $ a ['A']; is not better.

Why do we need to store variables in the database? your example shows that the value of Directly storing the field is 12345, and echo $ a ['A']; is not better.



In this case, the reason why I need to make an adaptive program in the database is that I need to store the function name and parameters in the database, and I can use it in the database, for example, if I have a function that calls func ($ B) and $ B is a variable, the value of this variable is "a = $ a", that is, to pass in such a string, $ a is a variable and can be set to any value. The function name func is saved in my database. the parameter a = $ a is combined into fun ("a = $.




This is okay. my problem is that I write the string a = $ a to the database, and then I can't identify $ a as a variable and use it as a string.

Use Eval.

$ Str = 12345;
Eval ("\ $ s = \" a = $ str \";");
Echo $ s;

Use Eval.



Thank you so much! you have saved me!

$ Str = 12345;
Eval ("\ $ s = \" a = $ str \";");
Echo $ s;




I just saw it. why don't you give me 10 points.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.