Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. is_array() / is_string() / is_numeric() / gettype()

is_array() / is_string() / is_numeric() / gettype() Since: PHP 4(2000)

Functions for checking the type of a variable. Use them to write safer code, such as validating form input or checking function arguments.

Syntax

// Checks whether a variable is an array.
is_array($value);

// Checks whether a variable is a string.
is_string($value);

// Checks whether a variable is numeric (or a numeric string).
is_numeric($value);

// Returns the type name of a variable as a string.
gettype($value);

Function List

FunctionDescription
is_array($value)Returns true if the variable is an array.
is_string($value)Returns true if the variable is a string.
is_int($value)Returns true if the variable is an integer. is_integer() and is_long() are aliases.
is_float($value)Returns true if the variable is a floating-point number. is_double() is an alias.
is_bool($value)Returns true if the variable is a boolean.
is_numeric($value)Returns true if the variable is a number or a numeric string. Accepts integers, floats, and numeric strings.
is_object($value)Returns true if the variable is an object.
gettype($value)Returns the type of the variable as a string. Possible values include integer, double, string, array, object, boolean, and NULL.

Sample Code

<?php
// Use is_array() to check whether a variable is an array.
$fruits = ['Apple', 'Orange', 'Grape'];
var_dump(is_array($fruits)); // Outputs: bool(true)
var_dump(is_array("hello")); // Outputs: bool(false)

// Use is_string() to check whether a variable is a string.
var_dump(is_string("Hello")); // Outputs: bool(true)
var_dump(is_string(123)); // 123 is an integer, so outputs: bool(false)

// is_int() only matches true integer types.
var_dump(is_int(42)); // Outputs: bool(true)
var_dump(is_int("42")); // "42" is a string, so outputs: bool(false)

// is_numeric() also returns true for numeric strings.
var_dump(is_numeric(42)); // Outputs: bool(true)
var_dump(is_numeric("42")); // Numeric string, so outputs: bool(true)
var_dump(is_numeric("3.14")); // Decimal string, so outputs: bool(true)
var_dump(is_numeric("abc")); // Not a number, so outputs: bool(false)

// Use gettype() to get the type name of a variable.
echo gettype(100); // Outputs: integer
echo gettype(3.14); // Outputs: double
echo gettype("hello"); // Outputs: string
echo gettype(true); // Outputs: boolean
echo gettype(null); // Outputs: NULL
echo gettype([]); // Outputs: array

// Validate that a form input value is numeric.
$_POST['age'] = "25";
if (is_numeric($_POST['age'])) {
	$age = (int)$_POST['age'];
	echo "Age: " . $age; // Outputs: Age: 25
}

Notes

Type-checking functions let you safely verify that a variable holds the expected type. Values received from a form are always strings, so use is_numeric() rather than is_int() to validate numeric input. is_int() checks the actual type, so it returns false for the string "42".

gettype() is handy for debugging and dynamic type inspection, but when branching on type in production code, dedicated functions like is_array() tend to be more readable.

To convert a variable's type, use the cast operator. To check whether a variable exists or is empty, use isset() / empty().

If you find any errors or copyright issues, please .