Wednesday, 27 April 2016

Programming Equivalence

Some of the most rudimentary logic in programming deals in comparison, or equivalence. For instance, every time we use an If statement, we use a conditional expression to test for equivalence. For the most part, this is pretty much a no-brainer. Consider the following in PHP.

    <?php
        $a = 5;
        $b = 3 + 2;
        echo ($a == $b ? "true" : "false");
    ?>


    true


This is true because the variable $b is set to (3+2), which is 5. The value of $a is also 5, therefore $a and $b are numerically equal.

Now consider this.
    <?php
        $a = 5;
        $b = 2.5 * 2;
        echo ($a == $b ? "true" : "false");
    ?>


    true


Again, $a and $b are numerically equal. $b is set to (2.5*2), which is 5.0, which is the numerical equivalent of $a.

Does this then follow that $a is equal to $b? Mathematically, yes. In programming parlance, no. Try the same code, with the following change.
    <?php
        $a = 5;
        $b = 2.5 * 2;
        echo ($a === $b ? "true" : "false");
    ?>


    false


The extra Equal sign

When you use the === operator rather than the == operator, you are testing for one more condition - that not only must $a and $b be numerically equivalent, they must be of the same data type. $a is 5, which is an integer. $b is 5.0, which is a floating point!

Now, how about this...
    <?php
        $a = true;
        $b = 1;
        echo ($a === $b ? "true" : "false");
    ?>


    false


$a is true, and numerically, that is 1. $b is set to the integer value of 1. Numerically they are equal, but $a is a Boolean and $b is an integer.

    <?php
        $a = intval(true);
        $b = 1;
        echo ($a === $b? "true" : "false");
    ?>


    true


The result is true. That is because $b is the integer 1. $a has been set to the integer value of true, which is 1. Therefore they are equivalent, both numerically, and in data type.

Why is this important?

If you want to compare only numerical equality, you use the == operator. And in cases where you wish to compare total equivalence, you use the === operator.

Say, for instance, you wanted to tally the items in a shopping cart. The total amount will then be processed to see if the shopper is entitled to a free gift. For arguments' sake, let's set that cut-off point at $50.00.
ItemQtyUnit PriceSubtotal
Cherries30

0.35
10.50
Oranges
10
1.00
10.00
Grapefruit
5
2.05
10.25
Peaches
15
1.55
23.25

The total is $54.00, so....
    <?php
        if ($cart_total >= 50)
        {
            echo "Congratulations! You are eligible for a mystery gift!";
        }
    ?>

See the comparison operator? It didn't rely on an extra Equal sign. That is because it doesn't matter if the total is an integer or a floating point; what we are interested in is the numerical value, and nothing else.

For a counter-example, let's take a look at PHP's search_array() function. For this, let's declare an array, $arr.
    <?php
        $arr = array("cherry", "orange", "pear", "banana");
    ?>


Now, if we were to search for "cherry" within this array, what would we get?
    <?php
        echo (array_search("cherry", $arr));
    ?>


0


We get 0 because that's the index position of "cherry" within the array $arr. But what if we wanted simply to check for the existence of "cherry"?
    <?php
        echo (array_search("cherry", $arr) == false ? "true" : "false");
    ?>


true


This is incorrect, because it is untrue that "cherry" is not found in the array $arr. The function returned an integer 0 because that is the index value of "cherry", as shown earlier. However, we used the == operator to compare it to false, which has the same numerical value as 0.

Thus, to eliminate ambiguity, we do this.
    <?php
        echo (array_search("cherry", $arr) === false ? "true" : "false");
    ?>


false


Here, we explicitly state that we're looking for a Boolean 0, not just any 0 value.

To conclude...

The extra Equal sign in PHP may cause some confusion. Hopefully, after explaining the concept of equivalence, things will be that much clearer.
Thanks very much. I cherry-sh your readership!
T___T

No comments:

Post a Comment