Skip navigation.

That's an ODD piece of code....

Wonky Code

Fabel sent in today's WTF. It short, and it definately made me go WTF... trying to figure out what it does. After looking at it, and looking at it a little more, it appears to check if $x is even or odd. Here's what Fabel had to say:

Heh this wasnt written by any of us in house it was from someone elses code that got used in a credit card validation routine, i noticed it one day and literally went WTF i've never seen anyone use this method to see if a number is odd or not. I had to do a double take to realize what it was checking for im used to seeing either a modulus to check or just a if($x & 1) for checking an odd number.

if ($x/2 != floor($x/2)) {
$digit *= 2;

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

This clearly isn't the right way to do things, but it's really not that odd.

The author might have premonitions that $x/2 is converted to a floating point number. That ain't so bad a premonition, you know.

I have to admit I have done similar checks... when teaching myself BASIC in Grade 3 and 4 (elementary school.
Didn't know anything about modulus...

Definitely a WTF. Mosulus is how it's supposed to happen.

In other news, I'm leaving from Baltimore tonight to go to Philly by way of St. Louis.


Nice site. I blogrolled ya!


I have studied 4 books on PHP, the official PHP documentation, and countless other web sites for 5 years. I STILL don't know what ($x & 1) does.

I think the problem here is a lack of COMPREHENSIVE BEGINNER documentation on the subject. (And if you think that's bad, try finding something comprehensive for beginners concerning UNIX!)

& is a bitwise AND operator, meaning that for each bit of the values, AND-operation is made and value resulted from these operations is returned. In this case it's used to check if the value is even or not:
Binary representation of 1 (in 8 bits): 00000001
ANDing any (either) binary digit with 0 results 0, so ANDing with 00000001 results in 0, if the last bit of the compared number isn't 1, for example
2 in binary: 00000010
2 & 1:
00000001 &

So ANDing a value with 1 returns zero if the value is even and non-zero if the value is odd.

I don't think this subject even should be covered in PHP's documentation as it has more to do with mathematics and low level data representation. For this reason I also suggest using the modulus operator insteads.

I use modulus a lot for coloring output to resemble greenbar paper (odd lines in a slightly different color for readabilty across wide columns). Also handy for making X by Y grids.

Acually, thats (almost) quite smart. You would want it to be more like?
if($x=2*( round($x/2) ) ) {
That means....
if(something=2*( round(half of something) ) ) {
Well if its even, rounding it after its been divided wont change anything. Then its timesed back up. So if its even, something=something

Now if its odd, one half of the odd number being rounded will make a difference. Then youll end up with something like 3=4, ect.

I once used that equation for spitting out odd numbers in a loop, then I found out about for() "steps". Uhg. :-D

Post new comment


  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <p> <br>
  • Web and e-mail addresses are automatically converted into links.