以下为学习孔祥盛主编的《PHP编程基础与实例教程》(第二版)所做的笔记。
PHP类型转换分为类型自动转换和类型强制转换。
1.布尔型数据参与算数运算时,TRUE被转换为整数1,FALSE被转换为整数0;NULL参与算术运算时,被转换为整数0。
1 <?php 2 $a = TRUE; 3 $b = FALSE; 4 $c = NULL; 5 $d = $a + 1; 6 $e = $b + 1; 7 $f = $c + 1; 8 9 var_dump($d); //int 2 10 echo "<br/>"; 11 var_dump($e); //int 1 12 echo "<br/>"; 13 var_dump($f); //int 1 14 echo "<br/>"; 15 ?>
输出:
1 D:wampServerwwwApache服务器主目录practise例程.php:8:int 2 2 3 D:wampServerwwwApache服务器主目录practise例程.php:10:int 1 4 5 D:wampServerwwwApache服务器主目录practise例程.php:12:int 1
2.浮点数与整数进行算术运算时,将整数转换为浮点数后,再进行算术运算。
程序:
1 <?php 2 $a = TRUE; 3 $b = FALSE; 4 $c = $a + 1.0; 5 $d = $b + 1.0; 6 7 var_dump($c); //float 2 8 echo "<br/>"; 9 var_dump($d); //float 1 10 echo "<br/>"; 11 ?>
输出:
1 D:wampServerwwwApache服务器主目录practise例程.php:7:float 2 2 3 D:wampServerwwwApache服务器主目录practise例程.php:9:float 1
3.参与算术运算的字符串,只有以数字开头的字符串才会被认作数字。
程序:
1 <?php 2 $a = 1; 3 $b = "-4.01"; 4 $c = "4.2e6"; //算术运算时,将被转换成4200000 5 $d = "-4.1degree"; //算术运算时,将被转换成 -4.1 6 $e = $a + "6th"; //算术运算时,将被转换成 6 7 $f = $a + $b; 8 $g = $a + $c; 9 $h = $a + $d; 10 $i = $a + "degree"; //算术运算时,"degree"将被转换成 0 11 12 var_dump($e); //int 7 13 echo "<br/>"; 14 var_dump($f); //float -3.01 15 echo "<br/>"; 16 var_dump($g); //float 4200001 17 echo "<br/>"; 18 var_dump($h); //float -3.1 19 echo "<br/>"; 20 var_dump($i); //int 1 21 ?>
输出:
1 D:wampServerwwwApache服务器主目录practise例程.php:12:int 7 2 3 D:wampServerwwwApache服务器主目录practise例程.php:14:float -3.01 4 5 D:wampServerwwwApache服务器主目录practise例程.php:16:float 4200001 6 7 D:wampServerwwwApache服务器主目录practise例程.php:18:float -3.1 8 9 D:wampServerwwwApache服务器主目录practise例程.php:20:int 1
4.在进行字符串连接运算时
①整数、浮点数将被转换成字符串类型数据。
例如,12.3转换为字符串后为“12.3”。
②布尔型TRUE将被转换成字符串“1”,布尔型FALSE跟NULL将被转换成空字符串“”。
程序:
1 <?php 2 $a = 1; 3 $b = 1.02; 4 $c = TRUE; 5 $d = FALSE; 6 $e = NULL; 7 $f = "degree"; 8 $g = $a.$f; 9 $h = $b.$f; 10 $i = $c.$f; 11 $j = $d.$f; 12 $k = $e.$f; 13 var_dump($g); //string '1degree' (length=7) 14 echo "<br/>"; 15 var_dump($h); //string '1.02degree' (length=10) 16 echo "<br/>"; 17 var_dump($i); //string '1degree' (length=7) 18 echo "<br/>"; 19 var_dump($j); //string 'degree' (length=6) 20 echo "<br/>"; 21 var_dump($k); //string 'degree' (length=6) 22 echo "<br/>"; 23 ?>
输出:
1 D:wampServerwwwApache服务器主目录practise例程.php:13:string '1degree' (length=7) 2 3 D:wampServerwwwApache服务器主目录practise例程.php:15:string '1.02degree' (length=10) 4 5 D:wampServerwwwApache服务器主目录practise例程.php:17:string '1degree' (length=7) 6 7 D:wampServerwwwApache服务器主目录practise例程.php:19:string 'degree' (length=6) 8 9 D:wampServerwwwApache服务器主目录practise例程.php:21:string 'degree' (length=6)
5.在进行逻辑运算时,空字符串“”、字符串零“0”、整数0、浮点数0.0、NULL以及空数组array()将被转换成布尔型为FALSE,其他数据将被转换成布尔型TRUE(注意字符串“0.0”将被转换成布尔型TRUE)。
程序:
1 <?php 2 $a = ""; 3 $b = "0"; 4 $c = "0.0"; 5 $d = 0; 6 $e = 0.0; 7 $f = NULL; 8 $g = array(); 9 $h = $a && TRUE; 10 $i = $b && TRUE; 11 $j = $c && TRUE; 12 $k = $d && TRUE; 13 $l = $e && TRUE; 14 $m = $f && TRUE; 15 $n = $g && TRUE; 16 17 var_dump($h); //boolean false 18 echo "<br/>"; 19 var_dump($i); //boolean false 20 echo "<br/>"; 21 var_dump($j); //boolean true 22 echo "<br/>"; 23 var_dump($k); //boolean false 24 echo "<br/>"; 25 var_dump($l); //boolean false 26 echo "<br/>"; 27 var_dump($m); //boolean false 28 echo "<br/>"; 29 var_dump($n); //boolean false 30 echo "<br/>"; 31 ?>
输出:
1 D:wampServerwwwApache服务器主目录practise例程.php:17:boolean false 2 3 D:wampServerwwwApache服务器主目录practise例程.php:19:boolean false 4 5 D:wampServerwwwApache服务器主目录practise例程.php:21:boolean true 6 7 D:wampServerwwwApache服务器主目录practise例程.php:23:boolean false 8 9 D:wampServerwwwApache服务器主目录practise例程.php:25:boolean false 10 11 D:wampServerwwwApache服务器主目录practise例程.php:27:boolean false 12 13 D:wampServerwwwApache服务器主目录practise例程.php:29:boolean false