php 字符串 大括号,PHP中的字符串大括号
復雜(卷曲)語法
這并不是因為語法復雜,而是因為它允許使用復雜表達式。
任何具有字符串表示的標量變量、數組元素或對象屬性都可以通過此語法包括在內。只需將表達式以與字符串外顯示的方式相同的方式寫入,然后將其包裝在{和}..自{無法轉義,只有在$立即跟隨{..使用{\$得到一個字面上的{$..以下是一些明確的例子:<?php //?Show?all?errorserror_reporting(E_ALL);$great?=?'fantastic';//?Won't?work,?outputs:?This?is?{?fantastic}echo?"This?is?{?$great}";
//?Works,?outputs:?This?is?fantasticecho?"This?is?{$great}";echo?"This?is?${great}";
//?Worksecho?"This?square?is?{$square->width}00?centimeters?broad.";
//?Works,?quoted?keys?only?work?using?the?curly?brace?syntaxecho?"This?works:?{$arr['key']}";
//?Worksecho?"This?works:?{$arr[4][3]}";//?This?is?wrong?for?the?same?reason?as?$foo[bar]?is?wrong??outside?a?string.
//?In?other?words,?it?will?still?work,?but?only?because?PHP?first?looks?for?a
//?constant?named?foo;?an?error?of?level?E_NOTICE?(undefined?constant)?will?be
//?thrown.echo?"This?is?wrong:?{$arr[foo][3]}";
//?Works.?When?using?multi-dimensional?arrays,?always?use?braces?around?arrays
//?when?inside?of?stringsecho?"This?works:?{$arr['foo'][3]}";
//?Works.echo?"This?works:?"?.?$arr['foo'][3];
echo?"This?works?too:?{$obj->values[3]->name}";echo?"This?is?the?value?of?the?var?named?$name:?{${$name}}";
echo?"This?is?the?value?of?the?var?named?by?the?return?value?of?getName():?{${getName()}}";
echo?"This?is?the?value?of?the?var?named?by?the?return?value?of?\$object->getName():?{${$object->getName()}}";
//?Won't?work,?outputs:?This?is?the?return?value?of?getName():?{getName()}echo?"This?is?the?return?value?of?getName():?{getName()}";?>
總結
以上是生活随笔為你收集整理的php 字符串 大括号,PHP中的字符串大括号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黄金价格走势图怎么看?三种简单方法
- 下一篇: php原生session,利用Memca