Type casting means converting a value from one data type to another. PHP supports types such as string, integer, float, Boolean, array and object.
Type casting allows you to convert a value into another data type when needed.
Example:
<?php
$number = "100";
var_dump($number);
$number = (int) $number;
var_dump($number);
?>
Output:
string(3) "100"
int(100)
Initially, the value is a string. After casting
with (int), it becomes an integer.
Use (int) or (integer)
to cast a value to an integer.
<?php
$a = "25";
$b = 12.89;
$c = true;
$x = (int) $a;
$y = (int) $b;
$z = (int) $c;
var_dump($x);
var_dump($y);
var_dump($z);
?>
Output:
int(25)
int(12)
int(1)
Important points:
Use (float) or (double)
to convert a value to a floating-point number.
<?php
$a = "45.75";
$b = 20;
$x = (float) $a;
$y = (float) $b;
var_dump($x);
var_dump($y);
?>
Output:
float(45.75)
float(20)
A float can represent numbers with fractional parts. The exact display of floating-point values can vary depending on the value and PHP environment.
Use (string) to convert a value into
a string.
<?php
$age = 21;
$price = 150.50;
$name = (string) $age;
$amount = (string) $price;
var_dump($name);
var_dump($amount);
?>
Output:
string(2) "21"
string(6) "150.50"
The string representation of a float depends on
how PHP converts that value. In this example,
150.50 is typically represented as
"150.5", not "150.50",
because trailing zeroes are not guaranteed to remain.
To preserve two decimal places:
<?php
$price = 150.50;
$formattedPrice = number_format($price, 2);
echo $formattedPrice;
?>
Output:
150.50
Use (bool) or (boolean)
to convert a value into true or false.
<?php
$a = 1;
$b = 0;
$c = "";
$d = "Hello";
var_dump((bool) $a);
var_dump((bool) $b);
var_dump((bool) $c);
var_dump((bool) $d);
?>
Output:
bool(true)
bool(false)
bool(false)
bool(true)
In PHP, zero, an empty string, the string "0", an empty array and null are examples of values that convert to false. Most other values convert to true.
Use (array) to cast a value to an array.
When a scalar value is cast, PHP creates an array
containing that value.
<?php
$name = "Rahul";
$result = (array) $name;
print_r($result);
?>
Output:
Array
(
[0] => Rahul
)
This is useful for learning about type conversion, but casting a value to an array does not automatically create a meaningful list of related data.
Use (object) to cast a value to an object.
When a string is cast to an object, PHP creates a
standard object with a property named scalar.
<?php
$name = "Rahul";
$result = (object) $name;
var_dump($result);
echo $result->scalar;
?>
Output:
object(stdClass)#1 (1) {
["scalar"]=>
string(5) "Rahul"
}
Rahul
The object identifier shown by var_dump()
may differ between executions.
PHP can automatically convert values between types in certain operations. This is often called type juggling.
<?php
$a = "10";
$b = 5;
$result = $a + $b;
var_dump($result);
?>
Output:
int(15)
PHP recognizes the numeric string in this arithmetic operation and calculates the result as an integer.
Automatic conversion depends on the operation and the values involved. Invalid numeric strings may cause errors or warnings in modern PHP, so validate user input rather than relying on automatic conversion.
Type casting changes a value's type. Type checking helps you identify the type of a value.
| Function or Syntax | Purpose |
|---|---|
(int) $value |
Cast to integer |
(float) $value |
Cast to float |
(string) $value |
Cast to string |
(bool) $value |
Cast to Boolean |
gettype($value) |
Returns the type name as a string |
is_int($value) |
Checks whether a value is an integer |
is_string($value) |
Checks whether a value is a string |
Example:
<?php
$value = "100";
echo gettype($value);
echo "<br>";
$value = (int) $value;
echo gettype($value);
echo "<br>";
var_dump(is_int($value));
?>
Output:
string
integer
bool(true)
Complete these exercises using PHP:
Sample starting code:
<?php
$value = "250";
echo gettype($value);
echo "<br>";
$value = (int) $value;
echo gettype($value);
echo "<br>";
echo $value + 50;
?>
Expected output:
string
integer
300
1. Which syntax converts a value to an integer?
2. What is the result of (int) 12.89?
3. Which function returns the type name of a variable?