Home > Backend Development > PHP Tutorial > Parsing inside php double quotes

Parsing inside php double quotes

WBOY
Release: 2016-07-30 13:29:50
Original
1714 people have browsed it

<code><span>echo</span><span>"This works: {<span>$arr</span>['foo'][3]}"</span>;
<span>echo</span><span>"This works too: {<span>$obj</span>->values[3]->name}"</span>;
<span>echo</span><span>"This is the value of the var named <span>$name</span>: {<span>${$name}</span>}"</span>;
<span>echo</span><span>"value of the var named by the return value of getName(): {<span>${getName()}</span>}"</span>;
<span>echo</span><span>"value of the var named by the return value of \$object->getName(): {<span>${$object->getName()}</span>}"</span>;</code>
Copy after login

解析数组

<code><span>$juices</span> = array(<span>"apple"</span>, <span>"orange"</span>, <span>"koolaid1"</span> => <span>"purple"</span>);
<span>echo</span><span>"He drank some <span>$juices</span>[0] juice."</span>.PHP_EOL;
<span>echo</span><span>"He drank some <span>$juices</span>[koolaid1] juice."</span>.PHP_EOL;
// 有效,只有通过花括号语法才能正确解析带引号的键名
<span>echo</span><span>"This works: {<span>$arr</span>['key']}"</span>;
// 有效
<span>echo</span><span>"This works: {<span>$arr</span>[4][3]}"</span>;</code>
Copy after login

解析变量

<code><span>echo</span><span>"This is {<span>$great</span>}"</span>;
<span>echo</span><span>"This is <span>${great}</span>"</span>;
</code>
Copy after login

输出字符串

<code><span>$a</span> = <span>'aaaa'</span>;
<span>$c</span>[<span>'c'</span>] = <span>"ccc"</span>;
<span>$b</span> = <span>"i am '<span>$a</span>' and  '<span>$c</span>[c]' \" 我是双引号!\""</span>;
<span>echo</span><span>$b</span>; //i am <span>'aaaa'</span> and <span>'ccc'</span><span>" 我是双引号!"</span></code>
Copy after login

{}的解析

<code><span><span>class</span><span>beers</span> {</span><span>const</span> softdrink = <span>'rootbeer'</span>;
    <span>public</span><span>static</span><span>$ale</span> = <span>'ipa'</span>;
}
<span>$rootbeer</span> = <span>'A & W'</span>;
<span>$ipa</span> = <span>'Alexander Keith\'s'</span>;
<span>// 有效,输出: I'd like an A & W</span><span>echo</span><span>"I'd like an {${beers::softdrink}}\n"</span>;
<span>// 也有效,输出: I'd like an Alexander Keith's</span><span>echo</span><span>"I'd like an {${beers::$ale}}\n"</span>;</code>
Copy after login

解析对象

<code><span>// 有效</span>
echo "This <span>square</span> is {$<span>square</span>-><span>width</span>}00 centimeters broad.";</code>
Copy after login

版权声明:知识取之于民,用之于民!欢迎转载,转载请开头附上本文链接,不定期更新文章!

以上就介绍了php 双引号里面的解析,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template