PHP Reference ยท Functions and Classes
PHP is_* type-checking functions
Reference for PHP is_* functions: is_int, is_float, is_string, is_bool, is_array, is_null, is_numeric, is_callable, is_iterable and how they differ.
The is_* functions answer one question: does this value have this type? Each takes a single value and returns true or false. They never convert anything and never throw, so they are safe to call on any input.
Most check an exact type (is_int(), is_string()), while a few check a broader idea: is_numeric() accepts numeric strings, is_callable() accepts anything you can call, and is_iterable() accepts arrays and Traversable objects.
var_dump(is_int("5")); // bool(false) var_dump(is_numeric("5")); // bool(true)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
is_int(mixed $value): bool // alias: is_integer(), is_long() is_float(mixed $value): bool // alias: is_double() is_string(mixed $value): bool is_bool(mixed $value): bool is_array(mixed $value): bool is_object(mixed $value): bool is_null(mixed $value): bool is_resource(mixed $value): bool is_scalar(mixed $value): bool is_numeric(mixed $value): bool is_iterable(mixed $value): bool is_countable(mixed $value): bool is_callable(mixed $value, bool $syntax_only = false, ?string &$callable_name = null): bool
| Function | Returns true for |
|---|---|
is_int() |
An integer. 7.0 and "7" are not integers. |
is_float() |
A float, including NAN and INF. |
is_string() |
A string of any content. |
is_bool() |
Exactly true or false. |
is_array() |
An array (not ArrayObject). |
is_object() |
Any object, including closures and enum cases. |
is_null() |
Exactly null. An unset variable is also null but raises a warning when read. |
is_resource() |
An open resource. Returns false for a closed one. |
is_scalar() |
An int, float, string or bool. Not null, arrays, objects or resources. |
is_numeric() |
An int, a float or a numeric string (optional leading whitespace and sign, decimals, exponent). |
is_iterable() |
An array or an object implementing Traversable. |
is_countable() |
An array or an object implementing Countable. |
is_callable() |
A closure, function name, "Class::method" string, [object, "method"] array or invokable object that can be called from the current scope. |
Return value: bool. None of these functions throws.
Usage notes
is_numeric()is about strings."7.5","1e3"and a leading-space" 7"are numeric; PHP 8 also accepts trailing whitespace ("7 "), but"0x1A"and"abc"are not. Check the run above.is_int()does not accept"7". Form fields and query strings are always strings, so usefilter_var()withFILTER_VALIDATE_INTwhen you need to validate input.is_null()equals=== null. Pick whichever reads better; seeisset()to also treat unset variables as null without a warning.is_callable()depends on scope. A private method is callable from inside its class but not from outside, so the same array can give different answers in different places. The second argument checks only the syntax.is_iterable()andis_countable()replace older checks likeis_array($x) || $x instanceof Traversable. They were added in PHP 7.1 and 7.3 and match theiterabletype declaration.is_a(),is_subclass_of()andinstanceoftest class relationships; seeinstanceof.- Aliases:
is_integer()andis_long()are aliases ofis_int();is_double()is an alias ofis_float(). Prefer the short names. - Use
declare(strict_types=1)and type declarations for function arguments instead of checking every parameter by hand.