Jump to content

& in PHP - It's not the AND operator


stuartbates

Recommended Posts

I've been looking through the Joomla docs trying to understand how everything works and I've seen something that I don't understand and cannot find an answer to anywhere on the net.

 

The basic question is what is the ampersand used for in this conditional here:

 

if (!($mask & 1) && is_string($var)) {

 

The two in the middle are obviously the AND part of the conditional.  I know it can be used for creating references to variables but I don't think this is what is happening here.  I've seen people using it to test for odd and even numbers too but with no explanation of how it works/what it does.  The full code is below:

 

 

function _cleanVar($var, $mask = 0, $type=null)    {        // Static input filters for specific settings        static $noHtmlFilter    = null;        static $safeHtmlFilter    = null;        // If the no trim flag is not set, trim the variable                if (!($mask & 1) && is_string($var)) {            $var = trim($var);        }        // Now we handle input filtering                if ($mask & 2)        {            // If the allow raw flag is set, do not modify the variable                        $var = $var;        }        elseif ($mask & 4)        {            // If the allow html flag is set, apply a safe html filter to the variable                        if (is_null($safeHtmlFilter)) {                $safeHtmlFilter = & JFilterInput::getInstance(null, null, 1, 1);            }            $var = $safeHtmlFilter->clean($var, $type);        }        else        {            // Since no allow flags were set, we will apply the most strict filter to the variable                        if (is_null($noHtmlFilter)) {                $noHtmlFilter = & JFilterInput::getInstance(/* $tags, $attr, $tag_method, $attr_method, $xss_auto */);            }            $var = $noHtmlFilter->clean($var, $type);        }        return $var;    }

 

 

Link to comment
https://forums.phpfreaks.com/topic/213686-in-php-its-not-the-and-operator/
Share on other sites

Thanks for the prompt reply I'd looked at those and disregarded them! Found an article that makes sense of it all for me now - so thanks again.  In case anyone reads this and needs to know more http://www.litfuel.net/tutorials/bitwise.htm explains everything nicely

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.