What is the difference between int and intval in php

WBOY
Release: 2023-03-16 16:20:01
Original
3083 people have browsed it

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.

What is the difference between int and intval in php

The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer

What is the difference between int and intval in php

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

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(&#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

(int) cast

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

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!

Related labels:
php
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