What does a single ampersand ('&') operator do in PHP?

12,680

It is bitwise operator AND.

For example, ($a & $b) evaluates both $a and $b is turned "on" (i.e. equal to 1)

See this: http://www.php.net/manual/en/language.operators.bitwise.php

Share:
12,680
Jaskaran singh Rajal
Author by

Jaskaran singh Rajal

As a web developer, I pride myself on learning new technologies, continually improving my skill-set and adapting to industry changes. I believe in professionalism, and representing my project based to the highest degree possible. Conceptualist, with a sharp eye for fresh approaches while understanding critical projects include code optimized solutions to achieve desired objectives. The bulk of my experience has involved developing e-commerce stores using the technologies I've listed. In addition to a core strength in PHP development and payment gateways, I have implemented social media aspects to several sites with connectivity APIs including those from Facebook and Twitter.

Updated on June 04, 2022

Comments

  • Jaskaran singh Rajal
    Jaskaran singh Rajal almost 2 years
    $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
    print_r(array_filter($array1, "odd"));
    
    
    function odd($var)
    {
        // returns whether the input integer is odd//
    
        return($var & 1);
    }
    

    In the return value what do & operator ?? how it return odd number

  • Albzi
    Albzi about 10 years
    Add an explanation? He asked what it does, not just what it is.
  • Armel Larcier
    Armel Larcier about 10 years
    Or just close this one
  • Albzi
    Albzi about 10 years
    Closed anyway @ArmelLarcier
  • Raptor
    Raptor about 10 years
    Marked as close & added explanation :)
  • Bhargav Kaklotara
    Bhargav Kaklotara over 4 years
    & is bitwise operators which convert operands to bit-level and then perform operation. let me give an example. here are two operands 5 ( 0101 in bit level ) and 1 ( 0001 in bit level ). AND(&) operation of both the numbers will give o/p of 0001, and that will be same for all the odd numbers. so result will be turned on for all odd numbers.
  • Jaskaran singh Rajal
    Jaskaran singh Rajal almost 3 years
    @BhargavKaklotara Great