The intval function is a built-in PHP function used to convert a value to an integer. Its basic syntax is as follows:
<span><span><span class="hljs-title function_ invoke__">intval</span></span><span>(</span><span><span class="hljs-keyword">mixed</span></span><span> </span><span><span class="hljs-variable">$var</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$base</span></span><span> = </span><span><span class="hljs-number">10</span></span><span>): </span><span><span class="hljs-keyword">int</span></span><span>
</span></span>
$var: The data to be converted, which can be of any type.
$base: An optional parameter specifying the base for conversion, defaulting to 10 (decimal).
The intval function automatically converts the input variable. If the input is a numeric string, intval parses the string and returns the integer part. If the string cannot be parsed into a valid number, it returns 0. For non-numeric types (such as objects or arrays), intval also returns 0.
<span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>(</span><span><span class="hljs-title function_ invoke__">intval</span></span><span>(</span><span><span class="hljs-string">"123abc"</span></span><span>)); </span><span><span class="hljs-comment">// int(123)</span></span><span>
</span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>(</span><span><span class="hljs-title function_ invoke__">intval</span></span><span>(</span><span><span class="hljs-string">"abc123"</span></span><span>)); </span><span><span class="hljs-comment">// int(0)</span></span><span>
</span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>(</span><span><span class="hljs-title function_ invoke__">intval</span></span><span>(</span><span><span class="hljs-number">10.56</span></span><span>)); </span><span><span class="hljs-comment">// int(10)</span></span><span>
</span></span>
In the examples above, the intval function automatically removes non-numeric characters in the string, keeping only the valid numeric portion. If the string cannot be converted to a valid number, it returns 0.
PHP supports direct type casting to convert variables to a specified type. For integers, you can cast as follows:
<span><span>(</span><span><span class="hljs-keyword">int</span></span><span>) </span><span><span class="hljs-variable">$var</span></span><span>
</span></span>
Or you can use the (integer) syntax. This casting method works for any data type, as long as you explicitly specify the type, PHP will perform the conversion.
<span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>((</span><span><span class="hljs-keyword">int</span></span><span>) </span><span><span class="hljs-string">"123abc"</span></span><span>); </span><span><span class="hljs-comment">// int(123)</span></span><span>
</span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>((</span><span><span class="hljs-keyword">int</span></span><span>) </span><span><span class="hljs-string">"abc123"</span></span><span>); </span><span><span class="hljs-comment">// int(0)</span></span><span>
</span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>((</span><span><span class="hljs-keyword">int</span></span><span>) </span><span><span class="hljs-number">10.56</span></span><span>); </span><span><span class="hljs-comment">// int(10)</span></span><span>
</span></span>
Like intval, type casting also converts strings to their integer parts, ignoring invalid or non-numeric characters. For strings that cannot be converted, type casting returns 0.
Although both intval and type casting can convert variables to integers, their implementations and use cases differ. The main differences are:
Return Type: intval always returns an integer, while type casting also returns an integer but sometimes requires more flexibility, especially when dealing with complex type conversions (e.g., converting to arrays or objects).
Parameter Control: The intval function accepts an optional $base parameter, allowing you to specify the numeric base, while type casting does not provide base control.
Fault Tolerance: intval returns 0 for strings that cannot be converted to valid numbers, while type casting follows PHP's conversion rules (for example, casting null returns 0, but casting an empty array or object may trigger a warning).
When explicit base conversion is needed: If you need to convert a string to an integer in a specific base, intval is more suitable because it supports the $base parameter. For example, when dealing with binary, octal, or hexadecimal data, intval offers more control.
<span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>(</span><span><span class="hljs-title function_ invoke__">intval</span></span><span>(</span><span><span class="hljs-string">"1010"</span></span><span>, </span><span><span class="hljs-number">2</span></span><span>)); </span><span><span class="hljs-comment">// int(10), binary to decimal</span></span><span>
</span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>(</span><span><span class="hljs-title function_ invoke__">intval</span></span><span>(</span><span><span class="hljs-string">"A"</span></span><span>, </span><span><span class="hljs-number">16</span></span><span>)); </span><span><span class="hljs-comment">// int(10), hexadecimal to decimal</span></span><span>
</span></span>
Clear Intent: Using intval makes code more readable and clearly expresses the intention to convert to an integer. If you only want to convert a variable to an integer without worrying about other type conversions or complex cases, intval is a clear choice.
Handling Strings: intval is more explicit when handling strings, parsing numbers from left to right and ignoring invalid characters. For scenarios that don’t require strict type casting, intval is usually simpler.
Handling Non-Numeric Strings: intval returns 0 for non-numeric strings (e.g., "abc123"), which can help prevent errors or unexpected results.
Performance Requirements: Type casting is generally more efficient than function calls because it directly operates on the variable’s internal representation rather than invoking a function. For high-performance scenarios, type casting may be more suitable.
Other Type Conversion Needs: If you need to convert other types (e.g., from array to string, object to array, etc.), type casting is more flexible.
Simple Data Type Conversion: If you only need to convert a variable’s type without additional features or control, type casting can be more concise.