TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-11-2010, 03:42 PM   #1 (permalink)
The Visitor
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
haskabab is on a distinguished road
Default Don't know what this does

I was following a tutorial I found about some database class and it uses the following code:

Code:
if($i%2 != 0){
    /**/
}
My question is: what does the %(int) do? Divide the $i var?

Thanks in advance.
Send a message via MSN to haskabab
haskabab is offline  
Reply With Quote
Old 08-11-2010, 04:43 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

it checks if the number $i (I am guessing $i value's is a number) is odd or even/

The modular or mod (%) returns the reminder of the division, such as if the statement is 4%3 it would be the reminder of the 4/3 division which in this case example is 1.

On the example code you gave if anything divided by 2 doesn't return 0 then the nominator is odd. I think it is a mathematical way to find odd or even numbers.
tony is offline  
Reply With Quote
Old 08-12-2010, 02:54 PM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Yep, the modulo operator returns the remainder of division of one number by another.

The above code could be condensed slightly. Because PHP treats return values of anything other than False/Null or '0' etc as true, we can just do this:

PHP Code:
if($i 2)
{

__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 08-14-2010, 03:58 PM   #4 (permalink)
The Visitor
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
haskabab is on a distinguished road
Default

Thanks alot,

cheers
Send a message via MSN to haskabab
haskabab is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:04 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design