note 50153 added to function.fmod

From: Date: Sun, 20 Feb 2005 02:10:34 +0000
Subject: note 50153 added to function.fmod
Groups: php.notes 
Request: Send a blank email to php-notes+get-85257@lists.php.net to get a copy of this message
john at digitizelife dot com: Well not sure how your comment applys to fmod.. but their is a sure simpler way of coping with situations like this.. its called a bit field (bit masking) e.g. /* Categories */ bin dec cat 0001 - 1 - Blue 0010 - 2 - Red 0100 - 4 - Green 1000 - 8 - Yellow /* Permissions */ 0010 - 2 - Bob 0101 - 5 - John 1011 - 11 - Steve 1111- 15 - Mary to find out the permissions for each user you simple need to do a bitwise AND $steve_auth=11; function get_perm($auth) { $cats["Blue"]=1; $cats["Red"]=2; $cats["Green"]=4; $cats["Yellow"]=8; $perms=array(); foreach($cats as $perm=>$catNum) { if($auth & $catNum) $perms[$perm]=true; } return $perms; } print_r(get_perm($steve_auth)); /* returns Array ( [Blue] => 1 [Red] => 1 [Yellow] => 1 ) */ This is far simpler than your prime number idea, in fact you dont even need a function in any tests for the permmsions on a user you can do them directly using the bitwise and operator. You may want to read the following http://en.wikipedia.org/wiki/Bitmask http://uk2.php.net/manual/en/language.operators.bitwise.php ---- Manual Page -- http://www.php.net/manual/en/function.fmod.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50153 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50153&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50153&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50153&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50153&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50153&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50153&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85257) next »