Converting String to Int in PHP

The easiest way to convert strings to numbers in PHP is to use the (int/float/double)$variable cast, which directly converts the string to a number. You can also convert a string to a number using the intval($value, $base), floatval($value), number_format($num, $decimals) and settype($var, $type) functions. The intval() function converts a string to an integer, and the floatval() function converts a string to a floating-point value. The number_format() function converts a string to a number and returns the formatted number on success; if it fails, the function throws an E_WARNING error. The settype() function is used to set or change the type of an existing variable. In this PHP String to Int example, we convert a string to a number using the intval() function. Below you can see more string-to-int conversion examples with detailed descriptions of each method. Click Execute to run the PHP String to Int Example online and see the result.
Converting String to Int in PHP Execute
<?php
$string = "23";

echo intval($string);
?>
Updated: Viewed: 10338 times

What is PHP?

PHP is an open source scripting language used for web development that can be embedded in HTML code. PHP is known for its flexibility and large developer community. PHP can run on many operating systems including Windows, Linux and macOS and supports multiple databases such as MySQL, PostgreSQL and SQLite.

What is a string in PHP?

A PHP string consists of sequences of characters, where each character is represented by one byte. Since a byte can only be 256 characters long, Unicode strings are not natively supported in PHP. When creating a string in PHP, you can use single quotes ('...'), double quoted strings ("..."), and Heredoc syntax (<<<). PHP provides many built-in functions for replacing, splitting, concatenating, interpolating, and comparing strings. You can also convert string to lowercase, convert string to int, convert string to array, and find the length of the string.

What is int in PHP?

In PHP, an integer (int) is a data type used to represent whole numbers without decimal points. Integers can be specified in decimal, hexadecimal, octal, or binary notation. PHP also automatically converts numeric strings to integers when used in a numeric context, but if the string is not a valid numeric string, PHP interprets it as 0.

PHP Converting String to Int Examples

The following are examples of converting string to int in PHP:

Converting string to number using type casting

In PHP, we can change the type of a variable using the cast syntax: (int/float/double)$variable. In most cases, this is not required since PHP does an implicit type conversion in most cases.

PHP String to Int Example
<?php
$str = "23";

echo (int)$str;
?>

#output: 23

Converting string to integer

To convert a string to an integer in PHP, you can use the intval() function:

PHP intval() Example
<?php
$str = "15";

echo intval($str);
?>

#output: 15

Converting string to a floating-point number

To convert a string to a float in PHP, you can use the floatval() function:

PHP floatval() Example
<?php
$str = "15.5";

echo floatval($str);
?>

#output: 15.5

Converting string to int by changing variable type

To set or change the type of a variable in PHP, you can use the settype(variable, type) function. The function takes two parameters: the variable we want to change and the type of the variable we need (integer, float, string, array, object, null).

PHP settype() Example
<?php
$str = "791";

settype($str, 'integer');

echo $str
?>

#output: 791

See also