Menu

PHP Type Casting: String to Int, intval() and (int)

Convert a PHP string to an integer with (int) $str or intval($str); (int) "42" is 42 and (int) "12abc" is 12. Learn casting to int, float, bool, string and array, numeric strings, intval with a base, settype, and how to validate input instead of silently getting 0.

This page includes runnable editors - edit, run, and see output instantly.

To convert a value to another type, put the type in parentheses before it: (int) "42" gives the integer 42. The function form intval("42") does the same. Casting never fails: text that is not a number becomes 0, and a string that starts with digits keeps only those digits.

The casts PHP has are (int), (float), (string), (bool), (array) and (object). Each also has a function: intval(), floatval(), strval(), boolval().

String to int: what (int) and intval() return

PHP reads digits from the start of the string, after skipping leading whitespace, and stops at the first character that does not fit a number. Exponent notation counts as a number, a 0x prefix does not.

Note "12.7" becomes 12: casting to int cuts off the fraction, it does not round. Neither (int) nor intval() prints a warning here, whatever the input.

intval() with a base

intval($value, $base) reads the string in another number system. With base 0, PHP looks at the prefix: 0x for hex, 0b for binary, a leading 0 for octal.

Float to int: truncation, not rounding

(int) drops everything after the decimal point, toward zero. Round first if you want the nearest whole number, and watch the parentheses:

The last pair is the classic float trap: (0.1 + 0.7) * 10 is stored as 7.999999..., so truncating gives 7. Rounding first gives 8. The [math page](/docs/php/math) covers round, floor and ceil.

Casting to bool, string and array

To bool, only a handful of values become false: 0, 0.0, "", "0", null and []. Everything else is true, including "0.0", " " and "false".

To string, true becomes "1", while false and null both become "". Floats drop a trailing .0, and very large or small floats use exponent notation.

Casting an array to a string does not join it. This prints Tags: Array:

<?php
$tags = ["php", "web"];
echo "Tags: " . $tags;

and PHP adds a warning:

Warning: Array to string conversion in /home/index.php on line 3

Use implode(", ", $array) or json_encode($array) to turn an array into text.

Every cast at a glance

Pick a value to see what intval, (bool), strval, is_numeric and gettype return for it. Every cell was computed by PHP 8.3.

intval, (bool), strval and is_numeric for each value of $x
Point at or tap a cell to read it as PHP.
$xintvalboolstrvalis_numericgettype
"10"
"1abc"
"abc"
""
" "
"0"
"0.0"
"1e1"
"010"
1.5
true
false
null
[]
[1, 2]
Every cell computed by PHP 8.3.19.

Numeric strings and automatic conversion

A numeric string is a string that looks like a number: "12", "-3.5", "1e3", with optional whitespace around it. PHP converts these automatically in arithmetic, and is_numeric() tells you whether a string qualifies.

"12abc" + 1 still gives 13, but PHP 8 prints Warning: A non-numeric value encountered, and "abc" + 1 throws a TypeError. Cast or validate before doing arithmetic on user input.

settype() and gettype()

settype() changes the type of a variable in place and gettype() reports it. get_debug_type() returns the shorter names you see in error messages (int, float, string).

Most code uses a cast ($value = (int) $value;) instead, because it is shorter and reads the same.

Validate input instead of casting it

(int) never says no, so (int) "abc" silently becomes 0 and (int) "5 apples" becomes 5. For form input, use filter_var with FILTER_VALIDATE_INT: it returns the integer or false.

Compare the result with === false, because 0 is a valid integer and == false would reject it. The filter_var page shows ranges and other validators.

Frequently Asked Questions

How do I convert a string to an int in PHP?

Use (int) $str or intval($str); both give the same result for base 10. (int) "42" is 42, (int) "12abc" is 12 and (int) "abc" is 0. To reject input that is not a whole number, use filter_var($str, FILTER_VALIDATE_INT), which returns false instead of guessing.

What is the difference between intval() and (int) in PHP?

For base 10 they return the same result. intval() also takes a base: intval('ff', 16) is 255, and with base 0 it reads prefixes like 0x, 0b and a leading 0 for octal. Being a function, intval can also be passed as a callback, as in array_map('intval', $values).

What does (bool) "0" return in PHP?

false. The string "0" is one of PHP's falsy values, together with "", 0, 0.0, null and []. Every other string is true, including "0.0", " " and "false".

How do I convert a number to a string in PHP?

Use (string) $n, strval($n) or put it in a string: "$n". (string) 3.0 is "3"; for a fixed number of decimals use number_format($n, 2) or sprintf('%.2f', $n).

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED