Weakly Typed Language
PHP is a weakly typed language, meaning that a variable's type can be changed on the fly. This is perfectly acceptable:
$var = 234;
$var = 'string';
PHP, as needed, will also change a variable's type. This normally occurs when a variable of one type is provided to a function that expects data of a different type. It also happens when using some operators with variables.
Although it is a convenience that PHP will convert a variable's type as needed, you should be careful about relying upon this feature too much. Conversions between strings and numbers normally work as would be expected, but conversions among the other types--arrays and objects, for example--likely have meaningless results. The PHP manual covers all of the effects and implications of type conversion in detail.
To force type conversion in your code, use type casting.
|