PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
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:
To show you some of these rules, I wrote the following sample PHP script, TypeAutoConversion.php:
<?php # TypeAutoConversion.php #- Copyright 2003 (c) HerongYang.com. All Rights Reserved. # 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 in PHP 7 and higher, you should get output with some messages. This is because now the default message level is set to E_NOTICE. You can change the setting or modify the code to make sure all variables are defined before using them.
herong> \php-7.3\php TypeAutoConversion.php 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%": PHP Notice: A non well formed numeric value encountered in C:\herong\TypeAutoConversion.php on line 14 Notice: A non well formed numeric value encountered in C:\herong\TypeAutoConversion.php on line 14 float(6.28) 3.14 * "$2.0": PHP Warning: A non-numeric value encountered in C:\herong\TypeAutoConversion.php on line 15 Warning: A non-numeric value encountered in C:\herong\TypeAutoConversion.php on line 15 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(22) "Hello 0.33333333333333"
If you run this sample script in PHP 5 and lower, you should get output without any message:
herong> \php5.6\php TypeAutoConversion.php 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"
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
Variables, References, and Constants
►Expressions, Operations and Type Conversions
►Data Type Automatic Conversion
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS