php conversion of digital types includes: 1. Automatic type conversion, strings are converted to numbers, integers are converted to floating point numbers, floating point numbers are converted to integers with the decimal point discarded, and null values are converted to strings; 2. Strong type conversion, [intval()] is converted to integer type, [floatval()] is converted to floating point number.
PHP conversion number types are:
PHP is a weakly typed language, unlike Java, C and other languages. The difference between strongly typed languages is that weakly typed languages automatically convert data types, while strongly typed languages must declare types manually.
1) Automatic type conversion, five data types, four types of scalars and null can be automatically converted through operations. Boolean values participate in operations
true ---> 1 false ---> 0
//bool to int var_dump(true + 1);//2 true->1 var_dump(false + 1);//1 false->0 var_dump(null + 1);// 1 null-> 0 echo '<hr/>'; // bool to float var_dump(true + 1.0);// float 2 var_dump(false + 1.0);// float 1 var_dump(null + 1.0);// float 1 echo '<hr/>'; //string to int var_dump('123' + 1);//124 '123'->123 var_dump('abc123' + 1);//1 'abc123'->0 var_dump('123abcdefggggggggggg;8000' + 1);//124 var_dump('a123' + 1);//1 var_dump('1a123' + 1);//2 //php7.0版本所有进制都不转 var_dump('077abc'+1);// 78 077->77 八进制不转换 var_dump('0b11abc'+1);//1 //0b 不转换 var_dump('0xffhsahahhahah'+1);//1 0x不转换 //php 5.6以下都会转换为 0xff->255 echo '<hr/>'; //string to float var_dump('1.234abcdef'+ 1.0);//2.234 var_dump('1.234E3'+1.0);//1235 var_dump('1e5'+1.0);//2 100001 var_dump('1E-5'+1.0);//1.00001
2) Strong type conversion
Use brackets plus the target type for conversion(int)(integer) (bool)(boolean) (float)(real) (string) (array) (object)
settype() Permanent conversion type function (key point)
The second parameter is the type name you want to change
intval() Convert to integer type
floatval() Convert to floating point number
strval() Convert to string
<?php $a = 1; var_dump($a); var_dump((int)$a); var_dump((integer)$a); var_dump((bool)$a); var_dump((float)$a); var_dump((real)$a); var_dump((string)$a); var_dump((array)$a); var_dump((object)$a); var_dump($a); var_dump($a); var_dump($a); var_dump($a); var_dump($a); echo '<hr/>'; $b = 100; var_dump($b); //下面函数是永久有效的类型转换 settype($b,'string'); var_dump($b); var_dump($b); var_dump($b); var_dump($b); var_dump($b); var_dump($b); echo '<hr/>'; //下⾯的函数也是当次有效 和最开始的强制类型转换一样 只不过语法不同而已 $m = 200; var_dump(intval($m)); var_dump(floatval($m)); var_dump(strval($m)); var_dump($m); var_dump($m); var_dump($m);
Related learning recommendations:
The above is the detailed content of What are the number types that php converts?. For more information, please follow other related articles on the PHP Chinese website!