Type Casting


Type Casting

When declaring a variable in PHP, we do not need to define the data type, and the PHP parser does that for us automatically. However, the variable's data type can be changed automatically or manually. This is referred to as typecasting.

Casting may be divided into two categories.

  • Casting Implicit (Automatic)
  • Casting Explicitly (Manual)

Implicit Casting

Implicit casting is something PHP does base on your code, but you have no control over it. Dividing one integer by another integer might be either an integer or a float, and PHP makes the final choice. Take a look at the sample below.

<?php $x = 2; $y = 4; var_dump($x / $y); // 2/4 = 0.5 (Float) var_dump($y / $x); // 4/2 = 2 (Int) ?>

output:

float 0.5 int 2

Explicit Casting

We may cast a variable to multiple data types explicitly. For example, we may use the following formula to convert a float to an integer. (The decimal place will be removed.)

Float to Intege Explicit Casting in PHP

<?php $x = 5.35; $y = (int) $x; // cast $x to integer var_dump($y); ?>

output:

float 0.5 int 2

The data type you want to convert the variable to should be placed before the variable, inside parentheses.

The following types of casts are permitted:

Cast Description
(int) or (integer) To an integer, cast.
(float) or (double) or (real) Toss a float in the water.
(bool) or (boolean) To a boolean, cast.
(string) To a string, cast.
(array) To an array, cast.
(object) To an item, cast.

Explicit Casting Examples

1. Casting to an integer The casts (int) and (integer) are both valid. When converting a float to an integer, the decimal part is removed. As a result, be certain that this decline has no further consequences.

Float to Integer Explicit Casting in PHP

<?php $x = 5.35; $y = (int) $x; // cast $x to integer var_dump($y); ?>

output:

float 0.5 int 2

Strings can also be turned into integers. When gathering numeric data from forms, this might be beneficial. (See the PHP forms chapter for further information.)

String to Integer Explicit Casting in PHP

<?php $x = '25'; $y = (integer) $x; // cast $x to int var_dump($y);?>

output:

int 25

The following example demonstrates how casting may be used practically. Any string that begins with a number and ends with a phrase will return the beginning number as an integer when cast to an integer.

Explicit Casting in PHP - Integer to Phrase

<?php $string = '10 Animals'; $numberOfAnimals = (int) $string; echo $numberOfAnimals;?>

output:

10

2. Casting to a float Valid casts include (float), (double), and (real). They all convert the variable to the float data type.

There will be no data loss when converting an integer to a float since integers do not contain a decimal component.

Explicit Casting in PHP - From Integer to Float

<?php $x = 7; $y = (float) $x; // $y is 7, but float var_dump($y);?>

output:

float 7

3. Casting to a boolean The terms bool and boolean are interchangeable.

Explicit Boolean Casting in PHP

<?php $a = (bool) 0; $b = (bool) 5; $c = (bool) ''; $d = (bool) 'Hyvor'; $e = (bool) []; $f = (bool) [2,5]; $g = (bool) null; var_dump($a); // false var_dump($b); // true var_dump($c); // false var_dump($d); // true var_dump($e); // false var_dump($f); // true var_dump($g); // false $h = (boolean) 'Hyvor'; // boolean also valid var_dump($h); // true ?>

output:

boolean false boolean true boolean false boolean true boolean false boolean true boolean false boolean true

4. Casting to an array Any single variable can be added to an array with only one element.

Array Explicit Casting in PHP

<?php $a = (array) 5; $b = (array) 'Hyvor'; $c = (array) true; var_dump($a); // [5] var_dump($b); // ['Hyvor'] var_dump($c); // [true] ?>

output:

array (size=1) 0 => int 5 array (size=1) 0 => string 'Hyvor' (length=5) array (size=1) 0 => boolean true