strange expression/conditional (to me)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

strange expression/conditional (to me)

Post 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?
wurdup
Forum Commoner
Posts: 39
Joined: Thu Apr 01, 2010 11:36 am

Re: strange expression/conditional (to me)

Post 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.
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Re: strange expression/conditional (to me)

Post 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.
wurdup
Forum Commoner
Posts: 39
Joined: Thu Apr 01, 2010 11:36 am

Re: strange expression/conditional (to me)

Post by wurdup »

yeah sorry i think it means if /else. i havent used those expressions for years, you can see why really.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: strange expression/conditional (to me)

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: strange expression/conditional (to me)

Post by AbraCadaver »

I like the modulus, but I guess you can do:

Code: Select all

return is_int($num/2);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply