RU beehive logo ITEC dept promo banner
ITEC 325
2016fall
ibarland

homelectshws
D2Lbreeze (snow day)

php-types
php's slippery types

introduce the project—Project: requirements

summary

There are some things that are not an error, but should be. For example: echo $iNeverDefinedThisVariable + 47; or echo $notAnArray[9+$notAnIndex] + 47; or echo strlen(heyThisHasNoQuotesSoIsNotReallyAString);.

This means that if you ever mis-type a variable name, you won't get an error, but instead PHP will happily proceed as if nothing is wrong.
PHP does not have your back.

Solution: Fortunately, people figured out that PHP's default behavior is unforgiveable, and added a switch to turn the above three behaviours into errors, as they should be: error_reporting(E_ALL);. Put this line at the top of all your PHP files, and save hours of headaches later.

Types in php

PHP does have different types (including the standard booleans, ints, floats, strings, and arrays). However, it often plays exceedingly fast-and-loose with them — “type juggling” — which might makes it seem like PHP doesn't know about types.

Conversion between strings and integers:

echo "hello" + "world";
echo 12 + 3;
echo "12" + "3";
echo "12" + 3;
echo "12hello" + "3world7" + "nevermind92";

echo strlen(37);
     
Remember: PHP does not have your back.

Rationale:

The solution: If you have a string, use is_numeric along with type casting.

Booleans: Many values, if used in a boolean context, are "false-ish":

echo false == false;
echo false == 0;
echo false == 0.0;
echo false == "0";
echo false == array();
echo $someUndefinedVar == false;   // ?!
    
Be careful:
echo 0 == false;           // true, as just noted.
// == is not transitive (!):
echo 0 == "00";            // true.
echo false == "00";        // false?!

echo 0 == "-0";            // true.
echo false == "-0";        // false?!

echo 0 == 0.0;             // true.
echo false == "0.0";       // false?!
    

Caution: There are also the boolean operators with spelled-out names, AND and OR. Do not use them! They are just like &&, || except that they have unexpectedly low precedence (less than the assignment operators Use parentheses to force precedence if you are doing something unusual (or even, just use parentheses routinely, so that you never even need to think twice about precedence!).

Upshot: PHP has ===, which does comparison suppressing any type-casts. Using this avoids unexpected results; when you want to convert you can call conversion-functions explicitly.


1In human law, if you know something is wrong and don't report it, it's often criminal negligence. Why we don't hold PHP or certain other languages to the same standard, I don't know.      

2 So $x = false || true; is find; it means So $x = (false || true);, and $x gets the value true. But $x = false OR true; means ($x = false) OR true;, which means $x gets false but the entire line returns true if used in some bigger context. (Btw, using the result of assignment in a bigger context is rarely a good idea; it's a holdover from more primitive I/O libraries that didn't have any peek/hasNext functionality.)
Bottom line: use && and ||, and use parentheses if you need some unusual precedence.      

homelectshws
D2Lbreeze (snow day)


©2016, Ian Barland, Radford University
Last modified 2016.Sep.09 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.