PHP expression _3_5_ data type conversion _ Auto Switch

The following is learning Kong Xiangsheng editor of "PHP Programming Fundamentals tutorial and example" (second edition) notes made.

 

PHP type conversion into two categories automatic conversion and casts .

1. Boolean data involved in an arithmetic operation, TRUE is converted to an integer of 1, FALSE is converted into an integer of 0; NULL participation arithmetic operations, is converted to an integer of 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 ?>

Output:

. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 8: 2 int
 2  
. 3 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 10: int. 1
 . 4  
. 5 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 12: int 1

 

2. When the floating-point arithmetic operations with integer, floating-point convert integer, then the arithmetic operation.

program:

 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 ?>

 

Output:

. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 7: a float 2
 2  
. 3 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 9: a float . 1

 

3. participation string arithmetic, only numbers that begin with the string will be recognized as numbers.

program:

. 1 <? PHP
 2      $ A =. 1 ;
 . 3      $ B = "-4.01" ;    
 . 4      $ C = "4.2e6" ; // when arithmetic operation, will be converted to 4.2 million
 . 5      $ D = "-4.1degree" ; / / arithmetic when operation, will be converted to -4.1
 . 6      $ E = $ A + "6th" ; // arithmetic during operation, it will be converted. 6
 . 7      $ F = $ A + $ B ;
 . 8      $ G = $ A + C $ ;
 . 9      $ H = $ A + $ D ;
10      $ I = $ A + "Degree" ; // arithmetic during operation, "degree" is converted into 0
 . 11  
12 is      var_dump ( $ E );    // int. 7 
13 is      echo "a" ;
 14      var_dump ( $ F );    // a float -3.01 
15      echo "a" ;
 16      var_dump ( $ G );    // a float 4,200,001 
. 17      echo "a" ;
 18 is      var_dump ( $ H );    // a float -3.1
19     echo "<br/>";
20     var_dump($i);   //int 1
21 ?>

Output:

. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 12 is: int. 7
 2  
. 3 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 14: a float -3.01
 . 4  
. 5 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 16: a float 4200001
 . 6  
. 7 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 18 is: a float - 3.1
 . 8  
. 9 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 20: int 1

 

4. When performing string concatenation

① integers, floating point numbers are converted to a string type data.

For example, 12.3 is converted string is "12.3."

② Boolean TRUE will be converted into a string of "1", with Boolean FALSE NULL will be converted to an empty string "."

program:

 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 ?>

Output:

. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 13 is: String '1degree' (length =. 7 )
 2  
. 3 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine. PHP: 15: String '1.02degree' (length = 10 )
 . 4  
. 5 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 17: String '1degree' (length =. 7 )
 . 6  
. 7 D: \ wampServer \ www \ Apache server home \ practice doing \ routine .php:. 19: String 'Degree' (length =. 6 )
 . 8  
. 9 D: \ wampServer \ www \ Apache server home \ practice doing \ routine .php: 21 : String 'Degree' (length =. 6)

 

5. carrying out a logic operation, empty string "", a string of zero "0", an integer of 0, floating point 0.0, NULL and empty array array () will be converted to a boolean is FALSE, the other will be converted into data Boolean TRUE ( note string "0.0" will be converted to a boolean TRUE ).

program:

 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 ?>

Output:

. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 17: Boolean  to false 
2  
. 3 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 19: Boolean  to false 
. 4  
. 5 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 21 is: Boolean  to true 
. 6  
. 7 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 23 is: Boolean  to false 
. 8  
. 9 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 25: Boolean  to false 
10  
. 11 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 27: Boolean  to false 
12 is  
13D: \ wampServer \ www \ Apache server home directory \ practise \ routine .php: 29: boolean  false

 

 

Guess you like

Origin www.cnblogs.com/xiaoxuStudy/p/11800095.html