Page 1 of 1
strange expression/conditional (to me)
Posted: Wed Apr 14, 2010 1:01 pm
by dimxasnewfrozen
I wanted to check whether a number was odd so I found this function somewhere on the interweb that works great.
Code: Select all
function checkNum($num){
return ($num%2) ? TRUE : FALSE;
}
I've never seen an expression/conditional written this way. Is there a name for this type of methodology? I'd like to learn more about it. Is it new?
Re: strange expression/conditional (to me)
Posted: Wed Apr 14, 2010 1:07 pm
by wurdup
function checkNum($num){
return ($num%2) ? TRUE : FALSE;
}
in pseudo code this is
if $num mod 2 equals zero return true or false
% means modular so it's asking if number is divisable by 2. ? means then
so 3 % 2 = 1
4 % 2 = 0
** actually this code is very un user friendly. I wouldn't recommend using code like this if others are to read it.
Re: strange expression/conditional (to me)
Posted: Wed Apr 14, 2010 1:20 pm
by dimxasnewfrozen
Interesting. I understood the mod I just didn't know what ? or : meant in the condition.
That makes sense. I guess for this case it would be easy to use this type of logic but if you're doing large conditionals I can see where this would be a disaster to use.
Re: strange expression/conditional (to me)
Posted: Wed Apr 14, 2010 2:05 pm
by wurdup
yeah sorry i think it means if /else. i havent used those expressions for years, you can see why really.
Re: strange expression/conditional (to me)
Posted: Wed Apr 14, 2010 2:36 pm
by AbraCadaver
dimxasnewfrozen wrote:Interesting. I understood the mod I just didn't know what ? or : meant in the condition.
That makes sense. I guess for this case it would be easy to use this type of logic but if you're doing large conditionals I can see where this would be a disaster to use.
Scroll down to
Ternary Operator:
http://php.net/manual/en/language.opera ... arison.php
Re: strange expression/conditional (to me)
Posted: Wed Apr 14, 2010 2:47 pm
by AbraCadaver
I like the modulus, but I guess you can do: