Understanding PHP intval() Function and (int) Casting

This article explains the PHP intval() function syntax, parameters, return values, and edge‑case behavior, compares it with the (int) cast operator, and provides numerous code examples illustrating conversions from strings, floats, arrays, booleans, and large numbers.

php Courses
php Courses
php Courses
Understanding PHP intval() Function and (int) Casting

The PHP intval() function converts a mixed value to an integer. Its signature is intval(mixed $value, int $base = 10): int, where $value is the data to be converted and $base specifies the numeral system (default is decimal).

When $value is a string, intval() reads the leading numeric part until a non‑numeric character is encountered; a leading ‘-’ is recognized as a sign. If $value is a float, the function returns the truncated integer. The function returns an int and respects the platform’s integer limits (e.g., 2147483647 on 32‑bit systems).

Typical usage examples:

<?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 (octal)
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 (overflow)
echo intval('420000000000000000000')."<br>"; // 2147483647 (max int)
echo intval(42, 8)."<br>";            // 42 (base 8)
echo intval('42', 8)."<br>";          // 34 (octal interpretation)
echo intval(array())."<br>";          // 0
echo intval(array('foo','bar'))."<br>"; // 1
echo intval(false)."<br>";            // 0
echo intval(true)."<br>";             // 1
?>

PHP also allows casting using the (int) operator, which performs a similar conversion but is not a function call. Example casts:

<?php
echo (int)42;          // 42
echo (int)4.2;         // 4
echo (int)'42';        // 42
echo (int)'+42';       // 42
echo (int)'-42';       // -42
echo (int)042;         // 34 (octal)
echo (int)'042';       // 42
echo (int)1e10;        // 1410065408
echo (int)'1e10';      // 2147483647
echo (int)0x1A;        // 26
echo (int)42000000;    // 42000000
echo (int)420000000000000000000; // -1609564160 (overflow)
echo (int)'420000000000000000000'; // 2147483647
echo (int)array();     // 0
echo (int)array('foo','bar'); // 1
echo (int)false;       // 0
echo (int)true;        // 1
?>

Key takeaways:

The intval() function and the (int) cast produce the same results for booleans, integers, floats, strings, and arrays, staying within the maximum integer range.

When the argument is a string, intval() extracts the leading numeric portion; if the string starts with a minus sign, it is treated as a negative number.

The maximum 32‑bit signed integer value is 2147483647; values exceeding this limit are capped or wrap according to PHP’s internal handling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendtype castingintval
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.