Skip to main content

PHP Type Casting

Type casting means converting one data type into another.

PHP automatically converts data types when needed, but you can also manually convert them.


Why Type Casting?

Type casting is useful when:

✅ Receiving user input

✅ Working with databases

✅ Processing APIs

✅ Handling configuration files

✅ PocketMine plugin development


Automatic Type Conversion

PHP automatically converts types.

Example:

$a = "10";
$b = 5;

echo $a + $b;

Output:

15

PHP automatically converts "10" into an integer.


Manual Type Casting

PHP supports the following casts:

(int)
(float)
(string)
(bool)
(array)
(object)
unset()

Integer Casting


String → Integer

$number = (int) "50";

echo $number;

Output:

50

Float → Integer

$number = (int) 99.99;

echo $number;

Output:

99

⚠ Decimal part is removed.


Boolean → Integer

echo (int) true;

Output:

1
echo (int) false;

Output:

0

Float Casting

$number = (float) "50";

var_dump($number);

Output:

float(50)

Example:

$health = 20;

$speed = (float) $health;

String Casting


Integer → String

$name = (string) 50;

var_dump($name);

Output:

string(2) "50"

Boolean → String

echo (string) true;

Output:

1

Array → String

echo (string) [];

Produces:

Warning

Boolean Casting


Integer → Boolean

(bool) 1

Output:

true

(bool) 0

Output:

false

String → Boolean

(bool) "Hello"

Output:

true

(bool) ""

Output:

false

Array Casting


String → Array

$data = (array) "Aayan";

print_r($data);

Output:

Array
(
[0] => Aayan
)

Integer → Array

$data = (array) 10;

Output:

Array
(
[0] => 10
)

Object Casting


Array → Object

$user = [
"name" => "Aayan",
"age" => 18
];

$obj = (object) $user;

echo $obj->name;

Output:

Aayan

Null Casting

(int) null

Output:

0

(string) null

Output:

""

(bool) null

Output:

false

Type Juggling

PHP automatically changes data types.

Example:

echo "10" + "20";

Output:

30

Example:

echo "100 players";

PHP keeps it as a string.


Strict Types

Always use:

declare(strict_types=1);

Without strict types:

function add(
int $a
) {

}

add("10");

Works.


With strict types:

TypeError

Useful Functions


intval()

intval("50");

floatval()

floatval("5.5");

strval()

strval(100);

boolval()

boolval(1);

settype()

$value = "50";

settype($value, "integer");

Type Checking Functions

is_int()
is_float()
is_bool()
is_string()
is_array()
is_object()

Real World Examples


Form Input

$age =
(int) $_POST["age"];

Config Values

$port =
(int) $config["port"];

PocketMine

$health =
(int) $player->getHealth();

Economy Plugin

$money =
(float) $data["money"];

Memory Representation

String

"50"

(int)



50

Common Mistakes


$number = "abc";

echo (int) $number;

Output:

0

(bool) "false"

Output:

true

Many beginners think this becomes false.

Any non-empty string becomes:

true

Best Practices

✅ Validate before casting.

if (is_numeric($value)) {

}

✅ Use strict types.


✅ Use explicit casting.


Exercises


Exercise 1

Convert:

"100"

to integer.


Exercise 2

Convert:

10

to string.


Exercise 3

Convert:

[
"name" => "Aayan"
]

to object.


Challenge

Create:

$name = "50";
$health = "20";
$money = "1000.50";

Convert them into proper types.


Quiz

What is:

(int) "50"
Answer

50


What is:

(bool) ""
Answer

false


What is:

(bool) "false"
Answer

true


References

PHP Type Casting:

https://www.php.net/manual/en/language.types.type-juggling.php

PHP settype():

https://www.php.net/manual/en/function.settype.php


Next Chapter

➡ PHP Operators