Programming PHP How to check variable types in PHP

How to check variable types in PHP

How to check variable types in PHP - techbriefers.com

To check variable types in PHP, we have many variable handling functions available. Below is the list of functions I am going to explain.

Function list to check variable types.

  1. is_array()
  2. is_​bool()
  3. is_​double()
  4. is_​float()
  5. is_​int()
  6. is_​long()
  7. is_​integer()
  8. is_​numeric()
  9. is_​object()
  10. is_​resource()
  11. is_​null()
  12. is_​string()

is_​array() function in PHP

PHP is_​array() function checks whether the passed argument is an array type or not.
Returns true if argument is array, otherwise returns false.

$arr = [1, 2];
var_dump(is_array($arr));
/*
result is
bool(true)
*/

$arr = 'not array';
var_dump(is_array($arr));
/*
result is
bool(false)
*/

is_​bool() function in PHP

PHP is_​bool() function checks whether the passed argument is of Boolean type (true/false) or not.
Returns true if argument is Boolean type, otherwise returns false.

$var = 1;
var_dump(is_bool($var));
/*
result is
bool(true)
*/

$var = true;
var_dump(is_bool($var));
/*
result is
bool(false)
*/

is_double() function in PHP

PHP is_​double() function checks whether the passed argument is of double/float/real type or not.
Returns true if argument is double/float/real type, otherwise returns false.

is_​float() function in PHP

PHP is_​float() function is alias of is_​double() function.

is_​real() function in PHP

PHP is_​real() function is alias of is_​double() function.

$var = 1.3;
var_dump(is_double($var));
var_dump(is_​float($var));
var_dump(is_​real($var));
/*
result is
bool(true)
bool(true)
bool(true)
*/

$var = 02;
var_dump(is_double($var));
var_dump(is_​float($var));
var_dump(is_​real($var));
/*
result is
bool(false)
bool(false)
bool(false)
*/

is_​int() function in PHP

PHP is_​int() function checks whether the passed argument is is of type integer or not.
Returns true if argument is integer type, otherwise returns false.

is_​long() function in PHP

PHP is_​long() function is alias of is_​int() function.

is_​integer() function in PHP

PHP is_​integer() function is alias of is_​int() function.

$var = 100;
var_dump(is_int($var));
var_dump(is_long($var));
var_dump(is_integer($var));
/*
result is
bool(true)
bool(true)
bool(true)
*/

$var = 10.0;
var_dump(is_int($var));
var_dump(is_long($var));
var_dump(is_integer($var));
/*
result is
bool(false)
bool(false)
bool(false)
*/

is_​numeric() function in PHP

PHP is_​numeric() function checks whether the passed argument is a numeric type value or not.
Returns true if argument is numeric type, otherwise returns false.

$var = 100;
var_dump(is_numeric($var));
/*
result is
bool(true)
*/
$var = '100';
var_dump(is_numeric($var));
/*
result is
bool(true)
*/

$var = 'hello';
var_dump(is_numeric($var));
/*
result is
bool(false)
*/

is_​null() function in PHP

PHP is_​null() function checks whether the passed argument is a null value or not.
Returns true if argument is null, otherwise returns false.

$var = 100;
var_dump(is_null($var));
/*
result is
bool(false)
*/
$var = '';
var_dump(is_null($var));
/*
result is
bool(false)
*/

$var = null;
var_dump(is_null($var));
/*
result is
bool(true)
*/

is_object() function in PHP

PHP is_object() function checks whether the passed argument is an object i.e, instance of class or not.
Returns true if argument is instance of class, otherwise returns false.

class  MyClass {

}

$obj = new MyClass();

var_dump(is_object($obj));
/*
result is
bool(true)
*/

is_resource() function in PHP

PHP is_resource() function checks whether the passed argument is a resource or not.
Returns true if argument is a resource, otherwise returns false.

$value = fopen(__DIR__.'/text.txt','r');
var_dump(is_resource($value));
fclose($value);

is_​string() function in PHP

PHP is_​string() function checks whether the passed argument is a string or not.
Returns true if argument is a string, otherwise returns false.

$var  = "mystring";
var_dump(is_string($var));
/*
result is
bool(true)
*/

$var  = "101";
var_dump(is_string($var));
/*
result is
bool(true)
*/

$var  = 101;
var_dump(is_string($var));
/*
result is
bool(false)
*/

Leave a Reply