# Mastering Data Type Conversion in PHP Through Type Casting

By Dinesh Uprety
Published: 2024-04-10
Updated: 2024-04-10
Canonical: https://laranepal.com/blog/mastering-data-type-conversion-in-php-through-type-casting

# Converting Between Data Types Using Type Casting

Converting values from one data type to another is known as type casting. A variable can be evaluated once as a
different type by casting it to another. This is accomplished by placing the intended type in front of the variable to
be cast.

| Cast Operators                | Conversion |
|-------------------------------|------------|
| (array)                       | Array      |
| (bool) or (boolean)           | Boolean    |
| (int) or (integer)            | Integer    |
| (object)                      | Object     |
| (real) or (double) or (float) | Float      |
| (string)                      | String     |

**Table: Type Casting Operators**

Let’s consider several examples. Suppose you’d like to cast an integer as a double, like so:

```php
<?php
    $score = (double) 13; // $score = 13.0
?>
```

Type casting a double to an integer will result in the integer value being rounded down, regardless of the decimal
value. Here’s an example:

```php
<?php
    $score = (int) 14.8; // $score = 14
?>
```

What happens if you cast a string data type to that of an integer? Let’s find out:

```php
<?php
    $sentence = "This is a sentence";
    echo (int) $sentence; // returns 0
?>
```

While likely not the expected outcome, it’s doubtful you’ll want to cast a string like this anyway. PHP will convert
strings to a representative numeric value when used in a math operation or when the cast operation is used. Another
example is the string “123 house” that will be converted to the numerical value 123.

You can also cast a data type to be a member of an array. The value being cast simply becomes the first element of the
array, like so:

```php
<?php
    $score = 1114;
    $scoreboard = (array) $score;
    echo $scoreboard[0]; // Outputs 1114
?>
```

> **Note**: This shouldn’t be considered standard practice for adding items to an array because this only seems to work
> for the very first member of a newly created array. If it is cast against an existing array, that array will be wiped
> out, leaving only the newly cast value in the first position.

One final example: any data type can be cast as an object. The result is that the variable becomes an attribute of the
object, the attribute having the name scalar:

```php
<?php
    $model = "Toyota";
    $obj = (object) $model;
?>
```

The value can then be referenced as follows:

```php
<?php
    print $obj->scalar; // returns "Toyota"
?>
```

# Concussion

Above code effectively demonstrates the process of type casting in PHP using various data types and shows how to perform
type casting for arrays and objects as well. Additionally, it provides insights into potential pitfalls such as the
behavior of casting a string to an integer and the implications of casting to an array. Overall, it's a comprehensive
guide to understanding type casting in PHP.
