How to distinguish between intval() and (int) in PHP

autoload
Release: 2023-04-09 20:44:01
Original
2862 people have browsed it

This article mainly compares the functions and definitions of intval() type conversion and (int) forced type conversion with some examples to distinguish between the two.

1.intval() function

Syntax:

intval ( mixed $value , int $base = 10 ) : int
Copy after login
  • value is the quantity value to be converted into integer

  • base is the base used for conversion (not Default is decimal)

  • Return value: int type variable

PS: Unless value is a string, otherwise base will not work.

Example:

<?php
echo intval(42)."<br>";                      // 42
echo intval(4.2)."<br>";                     // 4
echo intval(&#39;42&#39;)."<br>";                    // 42
echo intval(&#39;+42&#39;)."<br>";                   // 42
echo intval(&#39;-42&#39;)."<br>";                   // -42
echo intval(042)."<br>";                     // 34
echo intval(&#39;042&#39;)."<br>";                   // 42
echo intval(1e10)."<br>";                    // 1410065408
echo intval(&#39;1e10&#39;)."<br>";                  // 1
echo intval(0x1A)."<br>";                    // 26
echo intval(42000000)."<br>";                // 42000000
echo intval(420000000000000000000)."<br>";   // 0
echo intval(&#39;420000000000000000000&#39;)."<br>"; // 2147483647
echo intval(42, 8)."<br>";                   // 42
echo intval(&#39;42&#39;, 8)."<br>";                 // 34
echo intval(array())."<br>";                 // 0
echo intval(array(&#39;foo&#39;, &#39;bar&#39;))."<br>";     // 1
echo intval(false)."<br>";                   // 0
echo intval(true)."<br>";                    // 1
?>
Copy after login

2. (int) forced conversion

Demonstration :

<?php
echo (int)42;     // 42
echo "<br>";
echo (int)4.2;    // 4
echo "<br>";                   
echo (int)&#39;42&#39;;   // 42
echo "<br>";                    
echo (int)&#39;+42&#39;;  // 42
echo "<br>";                  
echo (int)&#39;-42&#39;;  // -42
echo "<br>";                 
echo (int)042;    //  34
echo "<br>";                    
echo (int)&#39;042&#39;;  // 42
echo "<br>";                  
echo (int)1e10;   // 1410065408
echo "<br>";                    
echo (int)&#39;1e10&#39;; //2147483647
echo "<br>";                
echo (int)0x1A;   // 26
echo "<br>";                    
echo (int)42000000;// 42000000
echo "<br>";                
echo (int)420000000000000000000;  //-1609564160
echo "<br>"; 
echo (int)&#39;420000000000000000000&#39;; //2147483647
echo "<br>";

/*echo intval(42, 8)."<br>";                   
echo intval(&#39;42&#39;, 8)."<br>";              */
/*int的强制转换不是函数,所以无法实现*/  
                
echo (int)array();// 0
echo "<br>";                 
echo (int)array(&#39;foo&#39;, &#39;bar&#39;);//1
echo "<br>";     
echo (int)false;   //0
echo "<br>";              
echo (int)true;    //1
echo "<br>";                   
?>
Copy after login

3. Summary:

  1. ##int forced conversion and The intval() function remains consistent when facing boolean, int, float, array (not exceeding each the maximum value displayed for each type).

  2. 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.

  3. The maximum value of the int type is 2147483647Generally during type conversion, if the maximum value is exceeded, the maximum value is displayed, ( int) will display -1609564160.

The above is the detailed content of How to distinguish between intval() and (int) in PHP. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template