I have problems with flag detection in PHP.
<?php
class event
{
const click = 0x1;
const mouseover = 0x2;
const mouseenter = 0x4;
const mouseout = 0x8;
const mouseleave = 0x16;
const doubleclick = 0x32;
public static function resolve($flags)
{
$_flags = array();
if ($flags & self::click) $_flags[] = 'click';
if ($flags & self::mouseover) $_flags[] = 'mouseover';
if ($flags & self::mouseenter) $_flags[] = 'mouseenter';
if ($flags & self::mouseout) $_flags[] = 'mouseout';
if ($flags & self::mouseleave) $_flags[] = 'mouseleave';
return $_flags;
}
}
var_dump(event::resolve(event::click | event::mouseleave));
var_dump(event::resolve(event::mouseleave));
But it returns:
array (size=4)
0 => string 'click' (length=5)
1 => string 'mouseover' (length=9)
2 => string 'mouseenter' (length=10)
3 => string 'mouseleave' (length=10)
array (size=3)
0 => string 'mouseover' (length=9)
1 => string 'mouseenter' (length=10)
2 => string 'mouseleave' (length=10)
I’m new to bitwise operators, so it could be a problem with their definitions.
How do I fix this?
You are giving the values of the flags wrong; they are hexadecimal integer literals, so they should be
You could also give them as decimal numbers (without the
0xprefix), although this can be misleading to someone who reads the code: