PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Data Type Automatic Conversion

This section provides rules on how operands are converted to data types required by the operation. PHP always finds a way to convert data from any type to any other type.

When the PHP evaluating an operation, it will automatically convert the data type of an operand if it does not match the data type required by the operator. Here are some basic data type automatic conversion rules:

  • For logical operations, NULL, 0, or "" will be converted to FALSE. Any strings that can be parsed to 0 will also be converted to FALSE. Other values will be converted to TRUE.
  • For arithmetic operations, strings will be parsed into numeric values. TRUE will be converted to 1. FALSE will be converted to 0.
  • For comparison operations, if one operand is a numeric value, the other operand will be converted to a numeric value for a numeric comparison. If both operands are strings, it will be a string comparison.
  • For string operations, NULL will be converted to "". TRUE will be converted to "1". FALSE will be converted to "". Numeric values will be converted to decimal presentations.

To show you some of these rules, I wrote the following sample PHP script, TypeAutoConversion.php:

<?php # TypeAutoConversion.php
# Copyright (c) 2003 by Dr. Herong Yang. http://www.herongyang.com/
# 
   print "\n Logical operations:\n";
   print "    NULL==FALSE: "; var_dump(NULL==FALSE);
   print "    0==FALSE: "; var_dump(0==FALSE);
   print "    \"\"==FALSE: "; var_dump(""==FALSE);
   print "    \"0\"==FALSE: "; var_dump("0"==FALSE);
   print "    \"0 - zero\"==TRUE: "; var_dump("0 - zero"==TRUE);
   print "    \"zero\"==TRUE: "; var_dump("zero"==TRUE);

   print "\n Logical operations:\n";
   print "    3.14 * \"2.0\": "; var_dump(3.14 * "2.0");
   print "    3.14 * \"2.0%\": "; var_dump(3.14 * "2.0%");
   print "    3.14 * \"\$2.0\": "; var_dump(3.14 * "$2.0");
   print "    3.14 * TRUE: "; var_dump(3.14 * TRUE);
   print "    3.14 * FALSE: "; var_dump(3.14 * FALSE);

   print "\n Conparison operations:\n";
   print "    3.14 < \"Hello\": "; var_dump(3.14 < "Hello");
   print "    3.14 > \"Hello\": "; var_dump(3.14 > "Hello");
   print "    \"3.14\" > \"Hello\": "; var_dump("3.14" > "Hello");

   print "\n String operations:\n";
   print "    \"Hello \" . TRUE: "; var_dump("Hello " . TRUE);
   print "    \"Hello \" . FALSE: "; var_dump("Hello " . FALSE);
   print "    \"Hello \" . NULL: "; var_dump("Hello " . NULL);
   print "    \"Hello \" . 1.0/3.0: "; var_dump("Hello " . 1.0/3.0);
?>

If you run this sample script, you should get:

 Logical operations:
    NULL==FALSE: bool(true)
    0==FALSE: bool(true)
    ""==FALSE: bool(true)
    "0"==FALSE: bool(true)
    "0 - zero"==TRUE: bool(true)
    "zero"==TRUE: bool(true)

 Logical operations:
    3.14 * "2.0": float(6.28)
    3.14 * "2.0%": float(6.28)
    3.14 * "$2.0": float(0)
    3.14 * TRUE: float(3.14)
    3.14 * FALSE: float(0)

 Conparison operations:
    3.14 < "Hello": bool(false)
    3.14 > "Hello": bool(true)
    "3.14" > "Hello": bool(false)

 String operations:
    "Hello " . TRUE: string(7) "Hello 1"
    "Hello " . FALSE: string(6) "Hello "
    "Hello " . NULL: string(6) "Hello "
    "Hello " . 1.0/3.0: string(20) "Hello 0.333333333333"

Last update: 2005.

Sections in This Chapter

What Is an Expression?

What Is an Operation?

Precedence of Operations

Data Type Automatic Conversion

Dr. Herong Yang, updated in 2009
Data Type Automatic Conversion