Home >Backend Development >PHP Problem >What are the number types that php converts?

What are the number types that php converts?

coldplay.xixi
coldplay.xixiOriginal
2020-07-10 10:00:253361browse

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

  • 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 &#39;<hr/>&#39;;
    // 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 &#39;<hr/>&#39;;
    //string to int
    var_dump(&#39;123&#39; + 1);//124 &#39;123&#39;->123
    var_dump(&#39;abc123&#39; + 1);//1 &#39;abc123&#39;->0
    var_dump(&#39;123abcdefggggggggggg;8000&#39; + 1);//124
    var_dump(&#39;a123&#39; + 1);//1
    var_dump(&#39;1a123&#39; + 1);//2
    //php7.0版本所有进制都不转
    var_dump(&#39;077abc&#39;+1);// 78 077->77 八进制不转换
    var_dump(&#39;0b11abc&#39;+1);//1 //0b 不转换
    var_dump(&#39;0xffhsahahhahah&#39;+1);//1 0x不转换
    //php 5.6以下都会转换为 0xff->255
    echo &#39;<hr/>&#39;;
    //string to float
    var_dump(&#39;1.234abcdef&#39;+ 1.0);//2.234
    var_dump(&#39;1.234E3&#39;+1.0);//1235
    var_dump(&#39;1e5&#39;+1.0);//2 100001
    var_dump(&#39;1E-5&#39;+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)

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

<?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 &#39;<hr/>&#39;;
 $b = 100;
 var_dump($b);
 //下面函数是永久有效的类型转换
 settype($b,&#39;string&#39;);
 var_dump($b);
 var_dump($b);
 var_dump($b);
 var_dump($b);
 var_dump($b);
 var_dump($b);
 echo &#39;<hr/>&#39;;
//下⾯的函数也是当次有效 和最开始的强制类型转换一样 只不过语法不同而已
 $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:

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!

Statement:
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

Related articles

See more