Bitwise Comparison

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Bitwise Comparison

Post by Benjamin »

Be careful, on several systems I have tested, in php 5, some, but not all valid bitwise comparisons will return false if they are not type casted to integers. I had a few failing coming out of a database.

Code: Select all

if (2 & 5622)
{
    echo "ok<br>";
} else {
    echo "failed<br>";
}

if ((string) 2 & (string) 5622)
{
    echo "ok<br>";
} else {
    echo "failed<br>";
}
Post Reply