zignaltrack Posted May 31, 2008 Share Posted May 31, 2008 i have a piece of code in front of me that i need to work with and it, to me, looks like a optimised if statement.. bt m new to php n it doesnt make sense to me.. was hopin if anyone could just tell me what does this line mean? the define statements are define('ROLE_ANONYMOUS', 1); define('ROLE_REGISTERED', 2 | ROLE_ANONYMOUS); define('ROLE_PUBLISHER', 2 | ROLE_REGISTERED); define('ROLE_ADMIN', 4 | ROLE_PUBLISHER); and the line i was hoping anyone could help me understand is.. $this->role = ($this->admin == 1)? ROLE_ADMIN : ROLE_REGISTERED; anyone plz? Link to comment https://forums.phpfreaks.com/topic/108142-solved-optimized-if/ Share on other sites More sharing options...
jake2891 Posted May 31, 2008 Share Posted May 31, 2008 All this line means is that if admin ==1 then use roll admin else use role_registered its the same as if(admin==1){ roleadmin }else{ role_registered } does that help? $this->role = ($this->admin == 1)? ROLE_ADMIN : ROLE_REGISTERED; Link to comment https://forums.phpfreaks.com/topic/108142-solved-optimized-if/#findComment-554289 Share on other sites More sharing options...
soycharliente Posted May 31, 2008 Share Posted May 31, 2008 It's called a ternary operator. http://www.google.com/search?q=php+ternary+operator if ($this->admin == 1) { role = ROLE_ADMIN; } else { role = ROLE_REGISTERED; } Link to comment https://forums.phpfreaks.com/topic/108142-solved-optimized-if/#findComment-554290 Share on other sites More sharing options...
zignaltrack Posted May 31, 2008 Author Share Posted May 31, 2008 yep very much.. thanks all.. i had to have that info cos there are places or functions that made me want to confirm this.. for e.g function setAdmin($isAdmin) { $this->admin = $isAdmin? 1 : 0; if ($this->admin == 1) { $this->role = ROLE_ADMIN; } } this is to set the admin, cos admin is a field in the user table that can have a value of 0/1 stating notanadmin/isanadmin... what i dnt understand the need for the line $this->admin = $isAdmin? 1 : 0;?? what is this line trying to achive?! Link to comment https://forums.phpfreaks.com/topic/108142-solved-optimized-if/#findComment-554294 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.