QUICK CONTENTS:
1. Type Juggling
2. Type Casting
3. Debugging Variables
4. A Programmer's Echo
Now that you know how to create any variable you can dream of, and assign it any value you can dream of, there’s one huge problem left: incompatible data types. It’s nice that we have all these different data types, but because they are different, we can’t easily mix them together. How can you add an integer to a string? How can you divide a Boolean by a float? How can you compare an array of different values with another array of different values?
This chapter will talk about how PHP automatically converts data types to make them compatible, and what you can do to change or aid this process.
Type Juggling
PHP is a nice guy. When you give him several data of different type, he will try to convert them so they match together, and he won’t even tell you the bother he went through. The result is that you can mindlessly mash together variables of differing data type without errors, but the results can be unexpected. This is called type juggling.
If you put together two types, they will both be converted to the data type with highest importance. The order of type importance is:
Null => Boolean => String => Integer => Float
So, for example,
- Executing
0.5 + 1
will result in a float of value1.5
. - Executing
3 + "7 dwarfs"
will result in an integer with value10
. - The integer value of
null
,false
and“”
(empty string) is0
. - The integer value of
true
is1
.
What about arrays, objects and resources? Well, because there’s many different ways to combine two of them, there’s no predefined behaviour. How do you add an array to an array? Add all values separately, or simply append the other array to the back? Putting together values of these data types will result in an error, unfortunately.
Either way, remember that type juggling only converts a variable to a certain data type in that specific expression. The data type of the variable itself will remain untouched.
$variable_uno = 15; $variable_duo = true; //Integer has highest importance, so true is converted to 1 //And the following statement will print 16 echo $variable_uno + $variable_duo;
Type Casting
There’s two problems with type juggling: we have no control over how it converts the values, and the conversion isn’t permanent. Luckily, we can force PHP to convert a variable to a certain type with type casting. Two alternative syntaxes exist:
$variable = (type) value;
settype($variable, "type");
They do exactly the same – you can use what you like best. These type keywords are available:
Keyword(s) |
Description |
|
Casts to integer |
|
Casts to Boolean |
|
Casts to float |
|
Casts to string |
|
Casts to array |
|
Casts to object |
|
Casts to null |
|
Casts a string to binary string |
$username = "Ginty"; //The username has now become an integer with value 0 $username = (int) $username; //The username has now become a boolean with value false settype($username, "bool");
Debugging Variables
Now, we’re in a bit of a quandary here. If we were to echo
a variable, we would see its converted value. With all this type juggling and wizarding, how could we ever know the type of a variable?
Well, to retrieve the type of a variable (as a string), use
gettype($var);
Lots of times, however, you simply want to check if a variable is of a certain type. For example, if the user inputs a string, do this, but if not, do that. For this, we have a collection of so-called is_type functions, which returns true if a variable is indeed of type type, and false otherwise.
Function |
Description |
|
Checks if variable is an integer |
|
Checks if variable is a float |
|
Checks if variable is an integer or float |
|
Checks if variable is a Boolean |
|
Checks if variable is a string |
|
Checks if variable is a scalar (integer / float / string / Boolean) |
|
Checks if variable is an array |
|
Checks if variable is an object |
|
Checks if variable is a resource |
|
Checks if variable is null |
|
Checks if variable is callable (which means it’s a function – more on that later). |
$username = "Ginty"; echo gettype($username); //Prints "string" $is_username_valid = is_string($username); //Returns true, so yes the username is valid!
A Programmer's Echo
But, there’s one last problem here. (Last one, I promise.) Because it’s impossible to convert an array or object, we can’t even use echo
to see its value. It would just give us an error. It would, therefore, be nice if we had some functions that would print our variables in a more readable, programmer-friendly way.
Fortunately, PHP has just the thing for you. To print any variable in a readable way, use
print_r($var)
If you want to take it one step further, and print all the information about a variable (type, contents, size, etcetera), use one of the following two
var_dump($var);
var_export($var, true);
The difference is that the second function outputs information about the variable as valid PHP code. So, you could save that information inside a new variable and do something with it if you want.
$player = "Ginty"; $items = ["Potion", "Sword", "Gun", "Smartphone"]; $player_has_a_sword = true; var_dump($player); //Prints string(5) "Ginty" //It also helps display variables that can't be converted to meaningful strings var_dump($player_has_a_sword); //Prints bool(true) print_r($items); /* Prints the array in readable format; Array ( [0] => Potion [1] => Sword [2] => Gun [3] => Smartphone ) */