PHP Pandas: Casting

← Back to Index

Things don't always go the way you planned, do they? Variables aren't always the right type. Fortunately, with PHP we can use a method known as 'casting' to force one data type to be another.

Basic Usage

Let's say we have a string with the following content.

<?php

$panda = '3';

Sure, it's a 3, but internally PHP is holding it as a string, and treating it as a string. We can see this by dumping the value. If we use var_dump() on the $panda variable, we'll receive the following output

string(1) "3"

See? PHP knows. Sure, when you perform arithmetic on the above string, PHP will cast the value to a numeric one internally, but what if we want to do it ourselves? What if we want to ensure that the variable is holding an integer instead. This is where casting is useful.

In PHP, there are a number of functions to help cast, but as we've not started on functions yet, let's use the shortcuts that are available to us. We can cast a value into another data type by specifying the new data type during the assignment process. It sounds complicated, but it's actually quite simple. Let's take a look at an example in code.

<?php

// Set a string value.
$panda = '3';

// Cast to integer.
$panda = (int) $panda;

// Dump result.
var_dump($panda);

We set a string representation of the number three on the first line. There's nothing new there. On the next line we use rounded ( brackets ) containing a new data type to cast the provided value (in this case our variable) to whatever data type was placed between the brackets. We've decided that it should be an integer, so we've provided the type int to our casting brackets. We could also have provided the type integer, they are interchangeable, but I find int is much faster to type, and much neater!

Let's take a look at the result.

int(3)

Fantastic! Our panda is now an integer instead of a string. Shall we try casting the string to a float? Can you guess the syntax? It's simple!

<?php

// Set a string value.
$panda = '3';

// Cast to float.
$panda = (float) $panda;

// Dump result.
var_dump($panda);

That's right, we just use the type float instead of int. Let's take a look at the result.

float(3)

Great! Our value is now a float.

Our casts can also work in reverse. If we want a float value represented as a string, we could use the following example.

<?php

// Set a float value.
$panda = 3.4567;

// Cast to string.
$panda = (string) $panda;

// Dump result.
var_dump($panda);

If we dump out the result, we'll find that our float value for $panda is now a string.

string(6) "3.4567"

It's clear what these casting examples achieve. I think that it's what we expected of it. Arrays are a little more difficult, though. What do you think will happen if we try to cast a string to an array?

When we have a question such as this, it always makes sense to write a little snippet to experiment with. Sometimes in my day-to-day work I forget what will happen with complicated casts or arithmetic, and I will construct a small demo snippet just to confirm my own understanding.

<?php

// Set a string value.
$panda = 'Lushui';

// Cast to array.
$panda = (array) $panda;

// Dump result.
var_dump($panda);

We'll use the array type to cast to an array. So, what happened?

array(1) {
    [0]=> string(6) "Lushui"
}

Well that's interesting! It looks like PHP has wrapped our string within an indexed array. In other languages, a statement such as this might have split our string up into an array of characters (letters), but PHP doesn't have the concept of a character type, and so it handles the statement a little differently.

This wrapping of a string is actually fairly useful. If a piece of our code demands that the variable it uses is an array, then we can simply cast to an array to have the value wrapped.

What happens if we try to cast a float value to an array? I suppose it will wrap that too, shall we check?

<?php

// Set a float value.
$panda = 1.98765;

// Cast to array.
$panda = (array) $panda;

// Dump result.
var_dump($panda);

Let's run it!

array(1) {
    [0]=> float(1.98765)
}

Great! As we had expected, our float has been wrapped in an array too.

Don't be afraid to use casting to bully your values into the right data type. They are here to work for you, after all!

In the next chapter we'll be learning more about comments.

My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.

It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!