The difference between int and intval in php: 1. Int refers to the Integer type, and intval refers to the function used to obtain the integer value of a variable; 2. The forced conversion of int and the intval function are displayed in various types beyond When the maximum value of intval is a string, it returns the integer value represented by the number string before the first character in the string that is not a number. The int type has a specified maximum value.
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
intval() function is used to get the integer value of a variable.
intval() function returns the integer value of variable var by using the specified base conversion (default is decimal). intval() cannot be used with object, otherwise an E_NOTICE error will be generated and 1 will be returned.
Int in php refers to the Integer type
intval() function
Syntax:
intval ( mixed $value , int $base = 10 ) : int
value is the quantity value to be converted into integer
base is the base used for conversion (default is decimal if left blank)
Return value: int type variable
PS: Base will not work unless value is a string.
Example:
<?php echo intval(42)."<br>"; // 42 echo intval(4.2)."<br>"; // 4 echo intval('42')."<br>"; // 42 echo intval('+42')."<br>"; // 42 echo intval('-42')."<br>"; // -42 echo intval(042)."<br>"; // 34 echo intval('042')."<br>"; // 42 echo intval(1e10)."<br>"; // 1410065408 echo intval('1e10')."<br>"; // 1 echo intval(0x1A)."<br>"; // 26 echo intval(42000000)."<br>"; // 42000000 echo intval(420000000000000000000)."<br>"; // 0 echo intval('420000000000000000000')."<br>"; // 2147483647 echo intval(42, 8)."<br>"; // 42 echo intval('42', 8)."<br>"; // 34 echo intval(array())."<br>"; // 0 echo intval(array('foo', 'bar'))."<br>"; // 1 echo intval(false)."<br>"; // 0 echo intval(true)."<br>"; // 1 ?>
(int) cast
Demonstration:
<?php echo (int)42; // 42 echo "<br>"; echo (int)4.2; // 4 echo "<br>"; echo (int)'42'; // 42 echo "<br>"; echo (int)'+42'; // 42 echo "<br>"; echo (int)'-42'; // -42 echo "<br>"; echo (int)042; // 34 echo "<br>"; echo (int)'042'; // 42 echo "<br>"; echo (int)1e10; // 1410065408 echo "<br>"; echo (int)'1e10'; //2147483647 echo "<br>"; echo (int)0x1A; // 26 echo "<br>"; echo (int)42000000;// 42000000 echo "<br>"; echo (int)420000000000000000000; //-1609564160 echo "<br>"; echo (int)'420000000000000000000'; //2147483647 echo "<br>"; /*echo intval(42, 8)."<br>"; echo intval('42', 8)."<br>"; */ /*int的强制转换不是函数,所以无法实现*/ echo (int)array();// 0 echo "<br>"; echo (int)array('foo', 'bar');//1 echo "<br>"; echo (int)false; //0 echo "<br>"; echo (int)true; //1 echo "<br>"; ?>
Summary:
The coercion of int and the intval() function are consistent when facing boolean, int, float, and array (not exceeding the maximum value displayed by each type).
intval() If the parameter is a string, returns the integer value represented by the digit string before the first character in the string that is not a digit. If the first character in the string is ‘-’, counting starts from the second character. If the parameter is a dotted number, its rounded value is returned.
The maximum value of int type is 2147483647. Generally, during type conversion, if it exceeds this maximum value, it will be displayed as the maximum value. (int) will display -1609564160.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between int and intval in php. For more information, please follow other related articles on the PHP Chinese website!