What are the number types that php converts?

coldplay.xixi
Release: 2023-03-02 09:08:01
Original
3265 people have browsed it

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.

What are the number types that php converts?

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
Copy after login

  • String and number operations, strings are first converted to numbers before operation.

  • Convert the string to a number, starting from the beginning and ending with the first character that is not a number (the non-matching content is cleared).

  • Integers are converted to floating point numbers, and the precision does not change.

  • Convert floating point number to integer, discard the decimal point, and keep the integer part.

  • If the floating point number is outside the integer range, the answer is undefined.

  • null value is converted to a string, which is an empty string.

  • //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 '
    '; // 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 '
    '; //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 '
    '; //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
    Copy after login

2) Strong type conversion

Use brackets plus the target type for conversion

(int)(integer) (bool)(boolean) (float)(real) (string) (array) (object)
Copy after login

Use type conversion function

settype()Permanent conversion type function (key point)

The first parameter is the variable of the type you want to change

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

'; $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 '
'; //下⾯的函数也是当次有效 和最开始的强制类型转换一样 只不过语法不同而已 $m = 200; var_dump(intval($m)); var_dump(floatval($m)); var_dump(strval($m)); var_dump($m); var_dump($m); var_dump($m);
Copy after login

Related learning recommendations:

PHP programming from entry to proficiency

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!