PHP{}塊級範圍詳解

來源:互聯網
上載者:User

本文主要和大家分享{}塊級範圍和and && or ||優先順序問題,希望能協助到大家。

and && or ||優先順序問題:

and or的優先順序低於&& || ,且低於=。
因此$b1 = $bA and $bB先運算的是$b1 = $bA

$bA = true;$bB = false;$b1 = $bA and $bB;$b2 = $bA && $bB;var_dump($b1); // $b1 = truevar_dump($b2); // $b2 = false$bA = false;$bB = true;$b3 = $bA or $bB;$b4 = $bA || $bB;var_dump($b3); // $b3 = falsevar_dump($b4); // $b4 = true

{}塊級範圍:

php中{}外面是可以取到其內部值的。PHP有函數範圍,但是沒有塊級範圍。

if(1){    $a = 123;}print_r($a);  // $a = 123;
for ($i = 0; $i < 10; $i++) {    for ($j = 0; $j < 10; $j++) {        $k = 777;    }}var_dump($j);//輸出10var_dump($k);//輸出777
$arr = [1, 2, 4];foreach ($arr as &$val) {    $val  *= 2;}$val = [];//重新賦值$val[0]=9;$val[1]=10;var_dump($arr,$val);輸出:array(3) {  [0]=>  int(2)  [1]=>  int(4)  [2]=>  &array(2) {    [0]=>    int(9)    [1]=>    int(10)  }}array(2) {  [0]=>  int(9)  [1]=>  int(10)}

由於$arr as &$val,迴圈到第三個元素,

val。雖然重新賦值為空白數組,但後面的修改還是會影響到了

val是一個引用,後面的修改都會影響到,除非加unset($val)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.