Lesson 10 of 50 – PHP Type Casting
20%

PHP Type Casting

Type casting means converting a value from one data type to another. PHP supports types such as string, integer, float, Boolean, array and object.

1. What Is Type Casting?

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.

2. Converting to 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:

  • A valid numeric string can become an integer.
  • Casting a positive float to an integer discards its fractional part.
  • When cast to an integer, true becomes 1 and false becomes 0.
3. Converting to Float

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.

4. Converting to String

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
5. Converting to Boolean

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.

6. Converting to Array

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.

7. Converting to Object

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.

8. Automatic Type Conversion

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.

9. Type Casting vs. Type Checking

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)
📝 Practical Assignment

Complete these exercises using PHP:

  1. Convert the string "250" into an integer.
  2. Convert the integer 100 into a string.
  3. Convert the float 45.89 into an integer.
  4. Convert the values 1 and 0 into Booleans.
  5. Convert the string "Hello PHP" into an array.
  6. Use gettype() to check the type of a variable before and after casting.
  7. Use is_int() to check whether a value is an integer.
  8. Calculate the sum of the numeric string "50" and the integer 25.

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

🧠 Quick Quiz

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?