Hudzilla.org - the homepage of Paul Hudson
Contents > Simple variables and operators Wish List | Report Bug | About Me ]

3.4     Forcing a type with type casting

This is NOT the latest copy of this book; click here for the latest version.

PHP will automatically convert data types as necessary across the board - you need not worry about it happening. If you specifically wish to override PHP's type conversion, you can perform what is called a type cast - you forcibly convert a variable of type A to type B. In PHP, type casting looks like this:

<?php
    $mystring
= "wombat";
    
$myinteger = (integer)$mystring
?>

At first, $mystring contains a string. However, we type cast it to be an integer, so PHP will convert it to an integer, and place the result into $myinteger. You can type cast as boolean using (bool), string using (string), and floating-point using (float).

Type casting is most often used to specifically enforce a type in order to provide extra security or just to make sure a set type of data is being used. For example, if your script absolutely requires an integer number, it's a smart move to typecast your variable with (integer) so that PHP will convert any other type to integer or do nothing if the type is already integer. Converting a float to an integer will automatically round the number down, and is actually faster than using the equivalent function.





<< 3.3 Automatic type conversion   3.5 Non-decimal number systems >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User[x] - 20 Aug 2008

Just a thought. Perhaps more information on
enforcing user input or a reference to where
in this book more information is found.
Thanks.

Jasen Betts - 20 Aug 2008

I see (integer) and (int) used interchangably, is that correct usage?

I think casts should be c-style: (integer)x
and not pascal-style: integer(x)

bye.

A PHP User - 20 Aug 2008

So integer("99wombat991") would still return 99 since PHP stops the numbering after it encounters a non-numeric character

Also the below code snippet is missing a ; before the echo line

Philippe - 20 Aug 2008

For the curious people the output of "wombat" as an integer is 0

<?php
$mystring = "99wombat";
$myinteger = (integer)$mystring
echo $myinteger
?>

gives 99 as output, putting the number anywhere else in the string keeps giving 0 as value.

Kyle - 20 Aug 2008

I'm curious - what is the output of typecasting "wombat" into an integer? Can you put this into the text?

A PHP User - 20 Aug 2008

you can also use (int) for integer casting
and (boolean) for boolean casting
Dirk



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow