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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.