Automatic type conversion occurs when two differently typed variables are combined in an expression or when a variable is passed as an argument to a library function that expects a different type. When a variable of one type is used as if it were another type, PHP automatically converts the variable to a value of the required type. The same rules are used for automatic type conversion as demonstrated previously in Table 2-1. Some simple examples show what happens when strings are added to integers and floats, and when strings and integers are concatenated: // $var is set as an integer = 115 Not all type conversions are so obvious and can be the cause of hard-to-find bugs: // $var is set as an integer = 39 Automatic type conversion can change the type of a variable. Consider the following example: $var = "1"; // $var is a string == "1"
Examining Variable Type and Content Because PHP is flexible with types, it provides the following functions that can check a variable’s type: boolean is_int(mixed variable) All the functions return a Boolean value oftrueorfalsedepending on whether the type of variable matches the variable type that forms the name of the function. For example, is_float( ) evaluates totruein the following code: $test = 13.0; While the PHP equals operator == tests the values of two variables, it doesn’t test the variables types. Consider the comparisons of string and integer variables: $stringVar = "10 reasons to test variable type"; Because of PHP’s automatic type conversion,$stringVar == $integerVarevaluates totrue. PHP provides the is-identical operator===that tests not only values, but types. In the fragment below, the expression$stringVar === $integerVarevaluates tofalse: $stringVar = "10 reasons to test variable type"; PHP also provides the is-not-identical operator,!==, that returnstrueif the value or type of two expressions are different. Debugging with gettype(), print_r(), and var_dump( )PHP provides the gettype( ), print_r( ), and var_dump( )functions, which print the type and value of an expression in a human-readable form: string gettype(mixed expression) These functions are useful for debugging a script, especially when dealing with arrays or objects. To test the value and type of$variableat some point in the script, the following code can be used: $variable = "3 Blind mice" + 39; This prints: int(42) While the var_dump( ) function allows multiple variables to be tested in one call, and provides information about the size of the variable contents, print_r( ) provides a more concise representation of arrays and objects, and will prove useful later when we start to use those variables. The gettype( ) function simply returns the type name for an expression: $variable = "3 Blind mice" + 39; The name that gettype( ) returns should only be used for information and not to programmatically test the type of a variable as the output is not guaranteed to remain stable with future PHP releases. To programmatically test a variable type, you should use the is_int( ), is_float( ), is_bool( ), is_string( ), is_array( ), or is_object( ) functions described earlier. The gettype( ), print_r( ), and var_dump( ) functions can be used on variables and expressions of any type, and we use them throughout the book to help illustrate the results of our examples. Testing, setting, and unsetting variablesDuring the running of a PHP script, a variable may be in an unset state or may not yet be defined. PHP provides the isset( ) function and the empty( ) language construct to test the state of variables: boolean isset(mixed var) isset( ) tests if a variable has been set with a non-null value, while empty( ) tests if a variable is equal to false. The two are illustrated by the following code: $var = 0; A variable can be explicitly destroyed using unset( ): unset(mixed var [, mixed var [, ...]]) After the call tounsetin the following example,$varis no longer defined: $var = "foo"; Table 2-2 show the return values forisset($var)andempty($var)when the variable$varis tested. Some of the results may be unexpected: when$varis set to"0,"empty( ) returnstrue. Table 2-2. Expression values
A variable is always set when it is assigned a value—with the exception of a null assignment—and isset( ) returnstrue. The empty( ) function tests the Boolean value of the variable and returns true if the variable isfalse. The statement $result = empty($var); is equivalent to $result = not (boolean) $var; However, PHP issues a warning when a cast operator is used on an unset variable, whereas empty( ) doesn’t.
blog comments powered by Disqus |
|
|
|
|
|
|
|