Primitive Type Functions

November 29th, 2009

The following are a list of primitive type functions that tests if a variable if of a given primitive type (boolean, integer, float, string, etc.). If the variable is of the type being tested a boolean of true is returned from the function, otherwise a false is returned.

Remember, that variables can be of different types but have the same value. For example, boolean of false has the same value as the integer zero. The function is_bool tests if an argument, a variable passed to a function, is a boolean type. An example is:

<?php

$isWalking = false;

if (is_bool($isWalking) === true) {
	echo("This variable is of type boolean.\n");
}
else {
	echo("This variable is not of type boolean.\n");
}

?>

Since $isWalkingis assigned a value false and false is a boolean type, the is_bool function returns true, and the program outputs: This variable is of type boolean. If I were to assign $isWalking a value of 0 which is of type integer, is_bool would return false. The program would output: This variable is not of type boolean.

Here is a list of function that tests the type of a given variable, $aVar. You can find an extensive list in the PHP manual.

Type Functions

  • is_bool($aVar) – Tests if $aVar is a boolean.
  • is_null($aVar) – Tests if $aVar is of type null, a type that has no value.
  • is_array($aVar) – Tests if $aVar is an array.
  • is_resource($aVar) – Tests if $aVar is a resource, a connection to a file, database, web server, etc.

Numeric Type Functions

  • is_int($aVar) – Tests if $aVar is an integer, a positive or negative whole number including zero.
  • is_integer($aVar) – Also tests if $aVar is an integer.
  • is_long($aVar) – Does the same thing as is_int. In languages such as C, a long type handles large integers.
  • is_float($aVar) – Tests if $aVar is a fractional positive or negative number, including 0.0. The value 0.0 is of type float and 0 an integer.
  • is_double($aVar) – Does the same test as is_float.
  • is_real($aVar) – Yes, another function that does the same thing as is_float.

String Type Functions

  • is_string($aVar) – Tests if $aVar is a string.
  • is_unicode($aVar)(PHP 6) Tests if $aVar is a unicode string. Unicode strings support an extended set of characters for foreign (non-English) languages. The buzzword for this is internationalization. At the time of this writing, I have never used is_unicode as PHP 6 does not exist yet. :-)
Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>