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

WBOY
Release: 2016-10-22 00:14:30
Original
2135 people have browsed it

&& 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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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.

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!