PHP and logical operators? -
i writing own permission class forum , i've run small problem can understand.
i have following function:
function edittopic($perm_edit_topic, $id, $user_id, $my_id, $permission){ if($perm_edit_topic == true && $user_id == $my_id or $permission == 0 xor $permission == 1){ echo '<a href="/forum/newtopic.php?edit='.$id.'" class="buttonpro" data-toggle="tooltip" data-placement="top" title="edit"><i class="fa fa-pencil-square-o fa-fw"></i></a>'; } }
i want if statement following:
if $perm_edit_topic
set true , $user_id
(which stored user_id
topic) user id of viewing user or permission id either 0 or 1 (where 0 admin , 1 moderator).
this works okay. owner of topic , admin , mods can edit. if set $perm_edit_topic
false
moderator, can still edit it.
did wrong in if
statement?
replace condition this:
$perm_edit_topic && ($user_id == $my_id || $permission == 0 || $permission == 1)
$perm_edit_topic == true
has exact same meaning$perm_edit_topic
wrap of other conditions inside of parenthesis control short circuiting, quite unintuitive how works combination of and'ing, or'ing, , xor'ing. if not familiar short circuiting try running example: does php have short-circuit evaluation?
inside parenthesis of conditions being true enough entire expression true. both
$permission == 0
,$permission == 1
cannot true, not make sense xor them
Comments
Post a Comment