PHP, the variable contains the variable!

Source: Internet
Author: User
PHP, the variable contains the variable! Php variables

The content of $ mcontent is "dear: {$ user ['username']} your purchased product: {$ goods_name}, Order No. {$ goods_no} has been placed successfully, due to the recent popularity of this product, please pay in time! "


It also contains variables. I can echo $ mcontent; and cannot input any Variables. what can I do !!!


Reply to discussion (solution)

Paste the program segment to see

Strings and variables must be connected.
Try this.
$ Mcontent = "dear :{". $ user ['username']. "} the item you purchased :{". $ goods_name. "}, order number {". $ goods_no. "} the order has been placed successfully. due to the recent popularity of this product, please pay in time! ";

The content is read from the database, but not from the upstairs. you have thought about your method, and you can't read it from the database directly in the code!

$ Query = new IQuery ('MSG _ template'); $ query-> limit = "1"; $ query-> where = "id = 4 "; $ arra = $ query-> find (); $ mtitle = $ arra [0] ['title']; $ mopen = $ arra [0] ['open']; $ mcontent = $ arra [0] ['content']; $ madmin_open = $ arra [0] ['admin _ open']; if ($ madmin_open = 1) {$ madmin_email = $ arra [0] ['admin _ email '];} // $ mcontent = "dear :{". $ user ['username']. "} the item you purchased :{". $ goods_name. "}, order number {". $ goods_no. "} the order has been placed successfully. due to the recent popularity of this product, please pay in time! "; If ($ mopen = 1) {$ smtp = new SendMail (); $ error = $ smtp-> getError (); $ smtp-> send ($ email, $ mtitle, $ mcontent, $ madmin_email );}

$ Content = <
"Dear: {$ user [username]} your purchased item: {$ goods_name}, order number {$ goods_no} has been placed successfully. due to the recent popularity of this product, please pay in time! ";
EOF;
Echo $ content;

Not upstairs !!!!

Not upstairs !!!!

You can output these variables to see what the situation is.


I can't see the source code. I don't even know how the values in your $ mcontent come from ~~~ Only printing can be performed one by one.

// Simulate the data read from the database $ mcontent = <'txt 'dear: {$ user ['username']} your purchased item: {$ goods_name }, order No. {$ goods_no} has been placed successfully. due to the recent popularity of this product, please pay in time! TXT; // the variables involved must be self-secured. $ user ['username'] = 'body'; $ goods_name = 'XXX'; $ goods_no = '2016 '; // solution 1 many people fear eval $ mcontent = eval ("return \" $ mcontent \ ";"); echo $ mcontent; // solution 2 dynamically creates a function, the format is a bit confusing $ f = create_function ('$ user, $ goods_name, $ goods_no', "return \" $ mcontent \";"); $ mcontent = $ f ($ user, $ goods_name, $ goods_no); echo $ mcontent; // or $ f = create_function ('$ user, $ goods_name, $ goods_no ', 'return "'. $ mcontent. '";'); $ mcontent = $ f ($ user, $ goods_name, $ goods_no); echo $ mcontent; // solution 3 template variable replacement, this is an interpreted template engine approach $ mcontent = preg_replace ('/{\$ [^}] +}/E',' $ 1', $ mcontent); echo $ mcontent;
Will get:
Dear: body
The product you purchased: xxx, Order No. 123 has been placed successfully. due to the recent popularity of this product, please pay in time!

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.