[ PHP ] – php 中 大括號 {} 的使用方法

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 2,平均評等: 5]

小編常看到別人寫的程式中都會在雙引號中使用 大括號 {} ,常常沒搞懂這種寫法;這次就花點時間了解一下用法。

//在字串中使用大括號 Use '{}' symbol in the string
echo '在字串中使用大括號 Use \'{}\' symbol in the string'.'<br>';
$str='混水摸魚';
echo '$str=\'混水摸魚\''.'<br>';
echo '$str{0}: '.$str{0}.'<br>';//出現錯誤了
echo '$str{0}.$str{1}.$str{2} : '.($str{0}.$str{1}.$str{2}).'<br>';//在utf8中的中文是由三個位元所組成 所以要一次輸出三個位元才會顯示正確的中文
$str1='How are you.';
echo '$str1=\'How are you.\''.'<br>';
echo '$str1{0}: '.$str1{0}.'<br>';
//在雙引號中使用大括號 Use '{}' symbol in the Double quotes
echo '在雙引號中使用大括號 Use \'{}\' symbol in the Double quotes'.'<br>';
$str2="哈囉你好嗎?$str".'<br>';
echo '$str2="哈囉你好嗎?$str"'.'<br>';
echo $str2.'<br>';
$str2="哈囉你好嗎?{$str}".'<br>';//明確指出雙引號中 變數的所在位置 會比較節省資源
echo '$str2="哈囉你好嗎?{$str}"'.'<br>';
echo $str2.'<br>';
//顯示結果
/*
在字串中使用大括號 Use '{}' symbol in the string
$str='混水摸魚'
$str{0}: �
$str{0}.$str{1}.$str{2} : 混
$str1='How are you.'
$str1{0}: H
在雙引號中使用大括號 Use '{}' symbol in the Double quotes
$str2="哈囉你好嗎?$str"
哈囉你好嗎?混水摸魚
$str2="哈囉你好嗎?{$str}"
哈囉你好嗎?混水摸魚
*/

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 2,平均評等: 5]
!去下營經續永站本助幫能就下一點輕輕,話的您到助幫有章文的我果如 如果我的文章有幫助到您的話,輕輕點一下就能幫助本站永續經營下去!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *