Type casting

Views: 8 Last modified: July 31st, 2011 Comments: 0

Type casting

Type casting in PHP is quite a convenient feature. This feature enables you to “convert” a particular variable which is type A to a specific type B. It allowes you for example to “convert” a integer variable to a string variable. We will practice with them soon!

First, you need to know the types were we can cast to.

  • int, integer
  • bool, boolean
  • float, double
  • string
  • array
  • object
  • unset

Syntax

Now you know the types we can cast to. All we have to do is discuss the syntax. It’s quite simple… A few examples:

(bool)$foo;
(boolean)$foo;
(int)$foo;
(integer)$foo;
(float)$foo;
(double)$foo;
(string)$foo;
(array)$foo;
(object)$foo;
(unset)$foo;

As you can see, the desired type must be written before the variable. That’s the way how it works. PHP will notice your type cast and transforms it to the desired variable type.

How to use

For example…. the booleans true and false are the same as 1 and 0. The boolean true(1) will be parsed on the screen in the most cases while the boolean false(0) wont be parsed as false/0 = nada/nothing!
This doesn’t mean you can’t parse the booleans as numbers.

Now we know that true and false are the same as 1 and 0, it’s quite obvious that we can cast to integers…

<?php
// Will return 1
echo (int)true;

// Will return 1
echo (integer)true;

// Will return 0
echo (int)false;

// Will return 0
echo (integer)false;
?>

Quite handy right ?

Let’s try the array!
The foreach structure can walk through an array…. When we give a single variable (string) as input, it will result in a warning:

<?php
$arr = 'Hello World';

foreach ($arr as $element)
	echo $element;
?>
Warning: Invalid argument supplied for foreach() in /home/vhosts/domain/httpdocs/test.php on line 6

We can fix this by type casting! Just let PHP put it into an array for you!

<?php
// Example 1
$arr = 'Hello World';
foreach ((array)$arr as $element)
	echo $element;

// Example 2
$arr = (array)'Hello World';

foreach ($arr as $element)
	echo $element;
?>

What happened ? Well, by using the (array) cast, we instructed PHP to read the variable as an array. You can check this by running the following snippet.

<?php
$arr = (array)'Hello World';
var_dump($arr);
?>

The result will be:

array(1) { [0]=> string(11) “Hello World” }

For the last example, we will use the string cast. Just imagine you have a number like 600 or 5844 or 25756.238, but somehow you need them in a string instead of using it as integer or float.

<?php
$int = 5835;
$float = 25.35;

echo (string)$int;
echo (string)$float;
?>

This will output:

5835
25.35

Now, you are not able to see if it worked. Just extend the above code with the piece of code below.

<?php
$int = 5835;
$float = 25.35;

$int = (string)$int;
var_dump($int);

var_dump((string)$float);

?>

The result will be:

string(4) “5835″
string(5) “25.35″

As you can see, they are now strings.

Generally, you won’t need this feature very often, however if you need it, you know its available! It’s quite simple and a quick solution for lots of issues!

VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {1+5} =  
    Anything to add ?

        You must be logged in to post a comment.