|
PHP is dynamically typed:
that is, it figures out the type
of values at run time —
whether an expression evaluates to a
(Contrast that with Java, which is statically typed: it knows the type of each value at compile-time, since the programmer is required to state the type each variable will hold, and the type returned by a function, and the type that must be passed in to each function.)
So a PHP variable can hold an int at one moment in time, and later hold a string; a function might return a string sometimes and return a boolean other times, etc.. Javascript, python, and racket are other dynamically-typed languages.
aside: Some people say that PHP is “untyped”; that is wrong. It is always aware of the type of any value. PHP has the types you might expect, including boolean, int, float, string, arrays, objects.1 Sometimes people (erroneously) say PHP is “weakly typed”. They might be trying to say “dynamically typed”, or they might be trying to say “PHP is particularly fast-and-loose about type conversions.”. It is true that PHP does significantly more “type juggling” than most sane languages.
Conversion between strings and integers:
var_dump( "hello" + "world" ); var_dump( 12 + 3 ); var_dump( "12" + "3" ); var_dump( "12" + 3 ); var_dump( strlen(37) ); |
Rationale:
The solution:
If you have a string,
use
Booleans: Many values, if used in a boolean context, are "false-ish":
var_dump( false == false ); var_dump( false == 0 ); var_dump( false == 0.0 ); var_dump( false == "" ); var_dump( false == "0" ); var_dump( false == array() ); |
var_dump( false == 0 ); // true, as just noted. // == is not transitive (!): var_dump( 0 == "00" ); // true. var_dump( false == "00" ); // false?! var_dump( "0" == false); var_dump( false == ""); var_dump( "0" == ""); // false?!, despite the previous two answers var_dump( 0 == "-0" ); // true. var_dump( false == "-0" ); // false?! var_dump( 0 == 0.0 ); // true. var_dump( false == "0.0" ); // false?! |
Caution: There are also the boolean operators with spelled-out names,AND andOR . Do not use them! They are just like&& ,|| except that they have unexpectedly low precedence (less than the assignment operators2 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
If you provide type-hints with your functions (perhaps in another file), there is a way to make the current file suppress type-juggling whenever it calls those function:
declare(strict_types=1); |
<php declare(strict_types=1); function my_strlen_untyped( $str ) { return strlen(strval($str)); } function my_strlen_typed( string $str ): int { return strlen($str); } var_dump(my_strlen_untyped(1234)); //returns 4 var_dump(my_strlen_typed(1234)); //ERROR, since we declare(strict_types=1). // It would be an error even if the function had been declared in a different file. // Likewise, it does NOT matter if the declaring-file had declare(strict_types=1). ?> |
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |