Home  >  Article  >  Backend Development  >  What does the $var & 1 syntax in array_filter mean?

What does the $var & 1 syntax in array_filter mean?

WBOY
WBOYOriginal
2016-10-22 00:14:302177browse

&& is and , what does one mean? It seems that it is not passed by reference, and then the result is 3. I don’t quite understand. Can anyone explain 2 sentences to me? Thank you

<code><?php
function test_odd($var)
{
return($var & 1);
}

$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));
?>

打印结果Array ( [3] => 3 )</code>

Reply content:

&& is and , what does one mean? It seems that it is not passed by reference, and then the result is 3. I don’t quite understand. Can anyone explain 2 sentences to me? Thank you

<code><?php
function test_odd($var)
{
return($var & 1);
}

$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));
?>

打印结果Array ( [3] => 3 )</code>

PHP’s bitwise operator - bitwise AND, test_odd is a function that determines odd numbers.

What does the $var & 1 syntax in array_filter mean?

But there is a very important sentence in this paragraph that is in English. Let me translate it:

If the two operands of &, |, ^ are both strings, then the string will be converted into the corresponding ascii code first and then the bit operation will be performed. If it is other cases, then They are all converted into integers and then performed bit operations.

So even though the ascii code of a is 97, it will still be filtered out.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn