note 89146 deleted from language.operators.bitwise by thiago

From: Date: Tue, 24 Feb 2009 13:38:16 +0000
Subject: note 89146 deleted from language.operators.bitwise by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-151056@lists.php.net to get a copy of this message
Note Submitter: vb (vb [at] bertola.eu) ---- I had a weird problem with bitwise operators applied to session variables in $_SESSION. I would check whether a logged user was authorized to perform an operation through a simple comparison between the bitmask of auth levels authorized for the operation and the user's auth level stored in $_SESSION: <?php $ok = ($allowed_auth & $_SESSION['auth']); ?> However, this got any sort of wrong and unpredictable results! In the end, I decided that something had to be going wrong with the way the PHP interpreter understood this operation, and I tried to typecast the session variable... et voilà: <?php $ok = ($allowed_auth & ((int) $_SESSION['auth'])); ?> This works perfectly. Bottom line: unless this is a bug that has been fixed in recent versions of PHP, you should typecast session variables to int before performing bitwise operations on them.

« previous php.notes (#151056) next »