intval() to get integer from data in php

We can use intval to get integer part
intval($value, $base );
$value : the data which integer is returned
$base : optional , base to consider

Examples using intval()

Code with output
echo intval(23); // 23
echo "<br>";
echo intval('23'); // 23
echo "<br>";
echo intval('23.34'); // 23
echo "<br>";
echo intval('023'); // 23
echo "<br>";
echo intval(023); // 19
echo "<br>";
echo intval('23',8); // 19
echo "<br>";
echo intval('2e10'); // 2
echo "<br>";
echo intval(2e10); // -1474836480

Example 1: Basic Usage of intval()

This example demonstrates how to convert a string with numeric content into an integer using intval().

$value = "123abc";
$int_value = intval($value);
echo "Converted value: " . $int_value;
Output:
Converted value: 123

Example 2: Handling Float Values

This example shows how intval() truncates the decimal part of a float.

$float_value = 45.67;
$int_value = intval($float_value);
echo "Truncated integer: " . $int_value;
Output:
Truncated integer: 45

Example 3: Using intval() with Different Bases

Use intval() to convert values from different base systems, such as hexadecimal to decimal.

$hex_value = "1A";
$int_value = intval($hex_value, 16);
echo "Hex to integer: " . $int_value;
Output:
Hex to integer: 26

ctype_digit() FILTER_VALIDATE_INT() is_numeric() is_numeric()
PHP Form ctype_alpha():Alphabetic chars only
Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer