simple problem i think.. modulus not working for 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
nilios
Forum Newbie
Posts: 2
Joined: Wed Mar 16, 2005 10:35 pm

simple problem i think.. modulus not working for me

Post by nilios »

before i start pulling out my hair can someone help me with this simple piece of code you've probably all have seen in any php beginner book.

for some reason i am not getting the correct result:

<?php
$x=6;
$y=4;
$z=$x%$y;
print $z;
?>

correct answer -> 5
my answer -> 2

why? maybe someone can help me out here.

thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you got the correct answer.. where did the "correct answer" you found come from?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

x mod y will give you the remainder of the divsion.
so if you divide 6 by 4, you will have 2 as remainder... (6 = 1 * 4 + 2)

thus the correct answer is 2 (and every x that can be written as x * 4 + 2, because they are in the same equivalence class).
nilios
Forum Newbie
Posts: 2
Joined: Wed Mar 16, 2005 10:35 pm

Post by nilios »

hey guys,

thanks for the help, i figured it out afterwards and was doing something totally different. need sleep.

later
Post Reply