Type Casting
As we know, PHP is more than capable of assigning data types to our variables based on the value assigned to the variable. However, if we update a variable to a value with a different data type, the data type gets altered. Take the following:
$age = "42";
echo gettype($age); // Output: string
$age = 42;
echo gettype($age); // Output: integer
The $age
variable goes from a string
to an integer
. But we're taking the extra step to update the variable just to change its data type. It's not uncommon to have to change the data type at a moment's notice. PHP supports typecasting to make this process easy.
What is typecasting?
Type casting in PHP is the process of converting a value from one data type to another in a way that you, the developer, explicitly control. In other words, type casting gives you more control over the data types of your values.
For example, if you have a string value "10"
and you want to perform arithmetic operations on it, you can explicitly convert it to an integer using type casting, like this:
(int) "10";
The name of the data type must be written before the value wrapped with parentheses. We can use the following type casts:
- cast to an integer by using
(int)
or(integer)
- cast to boolean using
(bool)
or(boolean)
- cast to float using
(float)
or(double)
- cast to string using
(string)
Type casting integers
There are a few things to keep in mind when casting non integers into an integer:
- Casting a boolean
false
converts into0
. Likewise, casting a booleantrue
convert into1
. - Casting float numbers will cause the PHP interpreter to round the decimal values toward zero.
- If a string is fully numeric, it'll be converted into the correct integer. However, strings with non-numeric characters will be converted into
0
. null
is converted into0
.
Here are some examples:
Value | Result |
---|---|
false | 0 |
true | 1 |
"-1" | -1 |
"null" | 0 |
"Hello" | 0 |
"12 months" | 12 |
"1.2e8" | 120000000 |
14.7 | 14 |
-5.9 | -5 |
Here are some code examples:
echo (int) false; // Output: 0
echo (int) true; // Output: 1
echo (int) "-1"; // Output: -1
echo (int) "null"; // Output: 0
echo (int) "Hello"; // Output: 0
echo (int) "12 months"; // Output: 12
echo (int) 1.2e8; // Output: 120000000
echo (int) 14.7; // Output: 14
echo (int) -5.9; // Output: -5
Type casting floats
There are a few things to keep in mind when casting non-floats into an float:
- Strings containing numbers will be transformed into the same numeric value. Anything else is transformed into
0
. - PHP transforms any other data types into an integer, which is then transformed into a float. Here are some examples:
Value | Result |
---|---|
false | 0 |
true | 1 |
"-1" | -1 |
"null" | 0 |
"Hello" | 0 |
"12.24 months" | 12.24 |
"1.2e8" | 120000000 |
Here are some code examples:
echo (float) false; // Output: 0
echo (float) true; // Output: 1
echo (float) "-1"; // Output: -1
echo (float) "null"; // Output: 0
echo (float) "Hello"; // Output: 0
echo (float) "12.24 months"; // Output: 12.24
echo (float) 1.2e8; // Output: 120000000
Type casting booleans
There are only two possible values that can be generated when type casting to a boolean, which are true
and false
. PHP transforms a value into false
based on the following conditions:
- When the integer value is
0
- When the float value is
0.0
- Empty strings
- The string
"0"
- When the value is
null
Value | Result |
---|---|
"false" | true |
0 | false |
"0" | false |
"zero" | true |
"" | false |
-1 | true |
Here are some code examples:
echo (bool) "false"; // Output: 1
echo (bool) 0; // Output:
echo (bool) "0"; // Output:
echo (bool) "zero"; // Output: 1
echo (bool) ""; // Output:
echo (bool) -1; // Output: 1
Booleans outputs
In the code example above, if we attempt to echo a true
value, PHP will output a string with the number 1
. A false
value produces an empty string. If you want to verify the data type, try using the gettype
function.
Type casting strings
There are a few things to keep in mind when casting non-strings into a string:
- A
false
boolean value becomes an empty string, and atrue
value becomes the string"1"
. - Integers and floats are converted to a textual representation of those numbers.
- The value
NULL
is always converted to an empty string.
Value | Result |
---|---|
false | "" |
true | "1" |
0 | "0" |
1.4545 | "1.4545" |
-5 | "-5" |
null | "" |
Here are some code examples:
echo (string) false; // Output:
echo (string) true; // Output: 1
echo (string) 0; // Output: 0
echo (string) 1.4545; // Output: 1.4545
echo (string) -5; // Output: -5
echo (string) null; // Output:
Exercise
Try type casting the following values:
- The string "Hello world" into a boolean.
- The boolean
true
into an integer. - The integer
55
into a float. - The float
54.55
into a string.
Key takeaways
- Type casting is the process of changing the data type of a value from one type to another.
- Developers can perform typecasting by specifying the type before the value wrapped with parentheses i.e.
(bool)
.