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.