Type Casting in PHP
PHP Data Types Simplified – Part 5
Forward: In this part of the series, we look at PHP Type Casting.
By: Chrysanthus Date Published: 10 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Casting
Casting is usually done in an assignment statement, where the left operand is a variable. The right operand is the value you want to change. In the casting statement, you precede the right operand with the new data type, in brackets. The casting types and what the right operand is cast into are, as follows:
(int), (integer) - cast to integer
(bool), (boolean) - cast to boolean
(float), (double), (real) - cast to float
(string) - cast to string
(object) - cast to object (see later)
(unset) - cast to NULL (PHP 5)
For the rest of this tutorial we shall look at examples of explanations of each of the above casting.
Casting to Integer
To cast to an integer you precede the right operand with (int) or (integer). Read and try the following:
<?php
$varFlt = 23.67;
$varInt = (int)$varFlt;
echo $varInt;
?>
When casting into an integer, the decimal part is just truncated. You are not obliged to use a variable in the right operand of the casting; you can use the value directly in place of the variable.
To cast to a bool you precede the right operand with (bool) or (boolean). A Boolean returned value of true is 1, which can be displayed. A Boolean returned value of false is 0, which may not be displayed. Read and try the following:
<?php
$varBl = (bool)2.5;
echo $varBl;
?>
When casting to boolean, the following values are cast as FALSE:
- the boolean FALSE itself
- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string, and the string "0"
- an array with zero elements
- the special type NULL (including unset variables)
- SimpleXML objects created from empty tags (see later)
Every other value is cast to TRUE (including any resource).
Casting to Float
You can cast an int to a float. You precede the right operand with (float), (double) or (real). The following illustrates this:
<?php
$varFlt = (float)25;
echo $varFlt;
?>
When you have a number, you can add another number to it to give a larger number. You can convert the number into a string. The converted number will still appear as a number with its same digits, but you will not be able to add any other number to it.
One way to convert a number to string, is to put quotes around the variable that holds the number or around the number itself. The following two code samples illustrate this:
<?php
$varNum = 12.5;
$varStr = "$varNum";
echo $varStr;
?>
<?php
$varStr = "12.5";
echo $varStr;
?>
Another way to convert a number into a string is to use the string casting operator. Read and try the following code:
<?php
$varNum = 12.5;
$varStr = (string)$varNum;
echo $varStr;
?>
<?php
$varStr = "12.5";
$varNum = (float)$varStr;
echo $varNum;
?>
<?php
$varNum = (float)"47";
echo $varNum;
?>
Casting to NULL
The casting approach here is a bit different. Assume that you have a variable, to which a value has been assigned. You can remove the assigned value from the variable. When you do that the variable will be left with no value. In this case the new value of the variable is NULL. You use the unset() function to achieve this. NULL is normally not displayed. Read and try the following code:
<?php
$var = 25;
unset($var);
echo $var;
?>
That is it for this part of the series. Let us stop here and continue in the next part.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course