In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to make strings parse functions directly in PHP. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The Writing of Direct Analytic function in PHP string
Strings in PHP cannot theoretically parse functions, only variables. Recently, it has been found that a special way of writing allows strings to parse functions directly.
Writing method
/ / single line ${! ${'} = code} / multiple lines ${! ${'} = code}
Personally, I think this kind of writing is actually realized by parsing variables. On the left side is a variable with a special name, and on the right side any code block that matches the assigned variable can be parsed in a string.
Example
The following code is for learning and communication only, and this method of writing is not recommended in practical work.
I.
$fruits = implode (',', ['apple',' banana']); var_dump ("fruits: $fruits."); / / parsing variables / / string (23) "fruits: apple, banana." var_dump ("fruits: implode (',', ['apple',' banana']).") / / function / / string (44) "fruits: implode (',', ['apple',' banana'])." var_dump ("fruits: ${! ${'} = implode (',', ['apple',' banana'])}.") / / string (23) "fruits: apple, banana." var_dump ("fruits: ${! ${'} = implode (',', ['apple',' banana'])}."); / / Multi-line writing is still successful / / string (23) "fruits: apple, banana."
II.
Var_dump ("fruits: ${! ${'} = $fruit? 'apple'}."); / / string (14) "fruits: apple." var_dump ("fruits: ${! ${'} = isset ($fruit)? $fruit: 'apple'}."); / / string (14) "fruits: apple."
3.
$fruit = function () {return 'banana';}; var_dump ("fruits: ${! ${'} = $fruit ()}."); / / string (15) "fruits: banana." var_dump ("fruits: ${! ${'} = call_user_func (function () {$fruits = ['apple',' banana']; return implode (',', $fruits);})}") / string (23) "fruits: apple, banana."
IV.
Class Fruit {public function _ toString () {return 'banana';}} var_dump ("fruits: ${! ${'} = new Fruit ()}."); / / string (15) "fruits: banana." Thank you for reading! This is the end of this article on "how to make string parsing function directly in PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 263
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.