Categories
Uncategorized

In PHP, There’s Equality, and Then There’s EQUALITY

Ka=Ping Yee’s Equality Test

Here’s a quickie PHP script based on the one that appears in Ka-Ping Yee’s LiveJournal entry titled Why PHP Should Never Be Taught:

<?php
$a = 0;
$b = "eggs";
$c = "spam";
$e = "eggs";

echo "<h1>The \"==\" Exercise</h1>";
echo "<ul>";
echo "<li>\$a is $a</li>";
echo "<li>\$b is $b</li>";
echo "<li>\$c is $c</li>";
echo "<li>\$d is undefined</li>";
echo "<li>\$e is $e</li>";
echo "</ul>";

echo ($a == $b) ? "\$a == \$b<br />" : "\$a != \$b<br />";
echo ($b == $c) ? "\$b == \$c<br />" : "\$b != \$c<br />";
echo ($a == $c) ? "\$a == \$c<br />" : "\$a != \$c<br />";
echo ($a == $d) ? "\$a == \$d<br />" : "\$a != \$d<br />";
echo ($b == $d) ? "\$b == \$d<br />" : "\$b != \$d<br />";
echo ($c == $d) ? "\$c == \$d<br />" : "\$c != \$d<br />";
echo ($b == $e) ? "\$b == \$e<br />" : "\$b != \$e<br />";
?>

If you’re not familiar with PHP’s quirks, you’ll find the output surprising:

The “==” Exercise

  • $a is 0
  • $b is eggs
  • $c is spam
  • $d is undefined
  • $e is eggs

$a == $b
$b != $c
$a == $c
$a == $d
$b != $d
$c != $d
$b == $e

What Happened?

In PHP, as with many other programming languages, the == operator is the equality operator, which returns true if the operands on either side are equal in value. It works as expected when used on operands of the same type, as evidenced by the program above, which states that $b is equal in value to $e, both of which are set to the string eggs.

We get into strange territory when the == operator is used to compare operands of different types. The program above evaluates the boolean $a == $b as true even though $a is set to the integer value 0 and $b is set to the value eggs. How can eggs be equivalent to 0? They’re so tasty and versatile! Damned anti-ovites!

In PHP, the == operator is what I like to call the “Slack Equality Operator”. When used to compare a string and a number, it attempts to convert the string to a numeric type and then performs the comparison. The following example code, taken from the PHP documentation, shows how PHP’s string-to-number coercion works:

<?php
$foo = 1 + "10.5";                // $foo is float (11.5)
$foo = 1 + "-1.3e3";              // $foo is float (-1299)
$foo = 1 + "bob-1.3e3";           // $foo is integer (1)
$foo = 1 + "bob3";                // $foo is integer (1)
$foo = 1 + "10 Small Pigs";       // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1;          // $foo is float (11)
$foo = "10.0 pigs " + 1.0;        // $foo is float (11)
?>

Hence the eggs/zero equivalence: the string eggs is coerced to 0.

Enter the === Operator

I like to call the === the “Strict Equality Operator”. It returns true if and only if:

  • Both operands are the same type
  • Both operands have the same value

Here’s the code I showed at the start of the article, but with all instances of == replaced with ===:

<?php
$a = 0;
$b = "eggs";
$c = "spam";
$e = "eggs";

echo "<h1>The \"===\" Exercise</h1>";
echo "<ul>";
echo "<li>\$a is $a</li>";
echo "<li>\$b is $b</li>";
echo "<li>\$c is $c</li>";
echo "<li>\$d is undefined</li>";
echo "<li>\$e is $e</li>";
echo "</ul>";

echo ($a === $b) ? "\$a === \$b<br />" : "\$a != \$b<br />";
echo ($b === $c) ? "\$b === \$c<br />" : "\$b != \$c<br />";
echo ($a === $c) ? "\$a === \$c<br />" : "\$a != \$c<br />";
echo ($a === $d) ? "\$a === \$d<br />" : "\$a != \$d<br />";
echo ($b === $d) ? "\$b === \$d<br />" : "\$b != \$d<br />";
echo ($c === $d) ? "\$c === \$d<br />" : "\$c != \$d<br />";
echo ($b === $e) ? "\$b === \$e<br />" : "\$b != \$e<br />";
?>

Here’s the output, which behaves as expected:

The “===” Exercise

  • $a is 0
  • $b is eggs
  • $c is spam
  • $d is undefined
  • $e is eggs

$a != $b
$b != $c
$a != $c
$a != $d
$b != $d
$c != $d
$b === $e

Once More, in Ruby

Just for kicks, I thought I’d translate the original code into Ruby just to see what would happen. Here’s the code:

a = 0
b = "eggs"
c = "spam"
e = "eggs"

puts "a is 0"
puts "b is 'eggs'"
puts "c is 'spam'"
puts "e is 'eggs'"

puts a == b ? "a == b" : "a != b"
puts b == c ? "b == c" : "b != c"
puts a == c ? "a == c" : "a != c"
puts a == d ? "a == d" : "a != d"
puts b == d ? "b == d" : "b != d"
puts c == d ? "c == d" : "c != d"
puts b == e ? "b == e" : "b != e"

…and here’s the output:

a is 0
b is 'eggs'
c is 'spam'
e is 'eggs'
a != b
b != c
a != c
double-equals.rb:14: undefined local variable or method `d' for main:Object (NameError)

Links

Categories
Uncategorized

Developer Hardware Nerdvana at TSOT

Gear-wise, life is pretty sweet at TSOT. The standard-issue computer is a 15″ MacBook Pro (the 2.2 Ghz model), which is a great Ruby on Rails development machine. The standard issue keyboard is the ultra-skinny Apple Wired Keyboard and Apple Mighty Mouse, but I don’t like the feel of either. I prefer the feel of the Logitech Cordless Wave Keyboard/Mouse combo, so that’s what got assigned to me.

The final piece of the developer gear complement arrived today: our Dell 24″ Ultrasharp widescreen LCD monitors. Now I’m in Hardware Nerdvana:

TSOT standard-issue developer gear: 15″ MacBook Pro, 24″ Dell widescreen LCD monitor and Logitech Wave cordless keyboard and mouse.

Categories
Uncategorized

TSOT’s Ruby/Rails Project Nights – Starting January 8th

Bruce Lee, wearing a TSOT t-shirt and holding Ruby on Rails nunchuks.

The Quick Version

TSOT Ruby/Rails Night
Tuesday, January 8th, 2008 (and the second Tuesday of every month)
@ TSOT’s office — 151 Bloor Street West (on the south side, just east of Avenue Road)
11th floor
Door open and food at 5:30 p.m.
Presentations start at 6-ish
FREE ADMISSION (but limited space)
To register, please email joey.devilla@tsotinc.com

About TSOT

TSOT is a Toronto-based start-up that develops — look out, here come the buzzwords — social networking applications using Ruby on Rails. Our first applications are FraternityLive and SororityLive, social software built specifically for people in fraternities and sororities. Both apps are currently being tested with a userbase of thousands of university students and alumni, and we expect to release them in early 2008.

About Ruby/Rails Project Nights

We believe that it’s good for Toronto to have a healthy developer ecosystem — it’s good not only for us as a Toronto-based development shop, but also as a group of developers who are passionate about the work we do. We’d like to see Toronto as “Silicon Valley++” — with the vibrant high-tech scene, but with all the amenities that make Toronto a better place to live than the Valley (such as not being a dreary 50-mile stretch of suburbia and having decent places to go at night).

Hence our contribution to the local developer scene: TSOT Ruby/Rails Project Nights, which will take place on the second Tuesday of every month. They’ll feature in-depth presentations by developers working on interesting projects — primarily Ruby and Ruby on Rails — along with drinks and munchies and a chance to socialize with your fellow developers. They’ll be hosted by Yours Truly, TSOT developer and DemoCamp regular Joey “Accordion Guy” deVilla.

The First Night: Tuesday, January 8th

This first Ruby/Rails Night will feature presentations by a couple of Ruby/Rails local heroes on their current Ruby/Rails projects:

The doors will open at 5:30, the first presentation will start at about 6, and we hope to wrap up the evening by 8:30 or 9. We’ll provide food and drinks, and if there’s enough of a demand, we can always go out to a nearby pub afterwards. There’s no cost to attend (but be advised that seating is limited).

If you’ve been thinking about making a Ruby or Rails presentation (perhaps you want to rehearse for RailsConf 2008!), we’d like to have you present it at one of our project nights!

Add TSOT Ruby/Rails Nights to your list of New Year’s resolutions!

How Do I Register?

Registration is free, but space is limited. To register for the upcoming Jan 8th gathering, please email joey.devilla@tsotinc.com

For More Information

For more information about TSOT Project Nights, please contact:

The event is also listed on Upcoming.org.