Welcome to Laranepal - Nepal's Laravel Community!

Mastering Data Type Conversion In Php Through Type Casting

Published on April 10, 2024 by

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
$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
$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
$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
$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
$model = "Toyota";
$obj = (object) $model;
?>

The value can then be referenced as follows:

<?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.

Dinesh Uprety

Senior Software Engineer • Writer @ Laranepal • PHP, Laravel, Livewire, TailwindCSS & VueJS • CEO @ Laranepal & Founder @ laracodesnap

Filed in:

Discussion

Login or register to comment or ask questions

No comments yet

Be the first to share your thoughts or ask a question.

Join the conversation

Sign in to share your thoughts with the community.