My check for divisibility is opposite to the expected result

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
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

My check for divisibility is opposite to the expected result

Post by robster »

The goal here is to get a new column <tr> for my table on every fourth entry in the database.

OK, this code below sits in a loop and basically I'm incrementing X and checking each time if X is divisible by 4.
The way I imagine the code to work, is it will print the word "test" and put in a <tr> every time it gets to a number divisible by four. When it's not divisible by four, I imagine it will print all the $title and $animator stuff.

What in actual fact it IS doing, is the complete opposite.

It's writing the word "test" and putting a <tr> in for EVERY time thru the loop except the times when X is divisible by 4.

How confusing...
Any ideas?

thanks :)

(below sits in a for loop that scans thru the database)

Code: Select all

if ($x % 4)  //is X divisible by 4?
				{  //if it is do this
					print "test<br>";
					print "<tr>";
				}
				else  //if it's not do this
				{
					print "<td valign="top">";
					print "<a href = "/files/$year/$month/$mpg"><img src=/files/$year/$month/$jpg></a>";
					print "<br>";
					print "$title<br>";
					print "<b>$animator</b><br>";		
					print "<br>";
					print "<br>";
					print "</td>";
				}			
			
		
				$x = $x + 1;
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

sorry all, i simply reversed the contents of the if else and it works... so obvious really.. :)

Still seems wierd tho.

Thanks for your interest :) Hope you learn something?


Rob
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

well
rob tells you how to fix it without tellign you what happened.


you see, traditionally in a true/false situation you use a single bite.
a single bite as far as a computer is physically concerned is a 1 or a 0 (binary)

thus, 1 is yes/true, and the default 2 is no/false.

but to simplify that, the computer checks that it is NOT zero.

thus, when the mod is 0 it's returning false and executing the else, and all other instances are true.

what you actually wanted was if(($x%4)==0){
which is telling it to execute WHEN the mod is equal to 0.

otherwise it looks at it as a true/false statement

this is something scripting languages inherited form their big bulky siblings, the programming lnaguages.



just more evidence that whoever said "we must know where we've been to know where we're going" was much wiser than myself

so many people overlook looking at where computers came from now -a-days, that's why it's good to have some theory/history courses in your education, by knowing how it's evoled you can sit down with something to teach you syntax and most quirks are logically expected because of the way computers and their languages developed.


then again, i'm a freak that can do binary progressions to the 20th place in my head nearly as fast as (wo)man with a calculator
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Thank you so much :)
That makes it all much clearer now...
Post Reply