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

3.3     Automatic type conversion

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

As PHP is typeless, it will automatically convert one type to another whenever possible. Problems with automatic conversion occur when either no meaningful conversion is possible, or when conversion yields unexpected results. For example, calling "print" on an array makes PHP print out "Array"; it doesn't automatically convert the array to a string of all its elements. Treating an object like a string has its own unique behaviour, but that's not too important right now.

The unexpected results occur when PHP converts values and produces unhelpful results. This is not PHP being badly written, more that your code needs to be more explicit. For example, converting from a boolean to a string will produce a 1 if the boolean is set to true, or an empty string if false. Consider this script:

<?php
    $bool
= true;
    print
"Bool is set to $bool\n";
    
$bool = false;
    print
"Bool is set to $bool\n";
?>

That will output the following:

Bool is set to 1
Bool is set to

As you can see, it hasn't printed out a 0 for false. Instead, nothing is printed, which makes the output look incorrect. To solve this problem, and others like it, tell PHP how you want the value converted by typecasting. The above script should be rewritten to typecast the boolean to an integer, as this will force boolean true to be 1 and boolean false to be 0.

<?php
    $bool
= true;
    print
"Bool is set to $bool\n";
    
$bool = false;
    print
"Bool is set to ";
    print (int)
$bool;
?>

This time the script outputs 1 and 0 as we wanted.





<< 3.2 Checking a variable is set: isset()   3.4 Forcing a type with type casting >>
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
firojshekh@rediffmail.com - 13 Oct 2008

Thanks
It really very good and very easy to use.

A PHP User - 13 Oct 2008

<script>alert(/Read it carefully!/)</script>

Rishi - 13 Oct 2008

It's very much simple and important book. I am begginer but could understand easily. Thank you book.

A PHP User - 13 Oct 2008

very good book

gdjoy - 13 Oct 2008

This book is very good!and once I convert false to string,get a ""!
thank your book!



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 eight plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow