php{} block-level scope detailed

Source: Internet
Author: User

This article is mainly to share with you {} block-level scopes and and && or | | Priority issues, hope to help everyone.

and && or | | Priority issues:

Priority of and or is lower than && | | , and below =.
So $b1 = $bA and $bB the first operation is $b1 = $bA .

$bA = true; $bB = false; $b 1 = $bA and $bB; $b 2 = $bA && $bB; Var_dump ($b 1); $b 1 = truevar_dump ($b 2); $b 2 = False$ba = false; $bB = true; $b 3 = $bA or $bB; $b 4 = $bA | | $bB; Var_dump ($b 3); $b 3 = falsevar_dump ($b 4); $b 4 = True

{} block-level scope:

Outside of {} in PHP, it is possible to fetch its internal values. PHP has a function scope, but no block-level scope.

if (1) {    $a = 123;} Print_r ($a);  $a = 123;
for ($i = 0; $i < $i + +) {for    ($j = 0; $j <; $j + +) {        $k = 777;    }} Var_dump ($j);//Output 10var_dump ($K);//Output 777
$arr = [1, 2, 4];foreach ($arr as & $val) {    $val  *= 2;} $val = [];//re-assigned $val[0]=9; $val [1]=10;var_dump ($arr, $val); output: Array (3) {  [0]=>  int (2)  [1]=>  Int (4)  [2]=>  &array (2) {    [0]=>    int (9)    [1]=>    int.  }}array (2) {  [0]=>  Int (9)  [1]=>  int (10)}

Because $arr as &$val , looping to a third element,

Val Although the re-assignment is an empty array, subsequent modifications will still affect the

Val is a reference, and subsequent modifications will affect it unless unset ($val) is added.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.