Mellonfire PHP Var Test Funcs
Mellonfire PHP Var Test Funcs
Version 1.0
July 28, 2006
By Melonfire
Takeaway
This document outlines the more useful functions available in a toolkit of PHP functions designed specifically to
test variables and find out if they belong to a particular character class.
Testing functions
Unlike many of its counterparts, PHP is not a strictly typed language. Essentially, this means that a developer
doesn't need to explicitly set the type (number, string, Boolean) of a variable before using it. Instead, the PHP
interpreter automatically detects variable type based on the information stored within a variable.
While this makes programming in PHP very easy, it does have an important drawback: when you do actually
need to test a variable's type, a loosely typed language can be somewhat confusing to deal with. Luckily, the
developers of PHP knew this and therefore included a toolkit of functions designed specifically to test variables
and find out if they belong to a particular character class - that is, whether they contains strings, integers, objects
or Booleans.
Table A outlines the more useful functions available in this category and provides explanations and usage
examples.
Page 1
Copyright 2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
Table A
Function
empty($var)
Explanation
This function is used to check if a
variable is empty (no value or a
zero value)
Use this function to check user
input -- for example, form
variables -- to ensure that they
contain valid data.
gettype($var)
is_bool($var)
is_string($var)
Example
<?php
// returns false
$var = "hello";
echo empty($var) ? "true" :
"false";
// returns true
$var = 0000;
echo empty($var) ? "true" :
"false";
?>
<?php
// returns string
$var = "hello";
echo gettype($var);
// returns double
$var = 1000.56;
echo gettype($var);
?>
<?php
// returns true
$var = false;
echo is_bool($var) ? "true" :
"false";
?>
<?php
// returns true
$var = "exception";
echo is_string($var) ? "true" :
"false";
// returns true
$var = "88408";
echo is_string($var) ? "true" :
"false";
?>
is_numeric($var)
<?php
// returns true
$var = "+99.766";
echo is_numeric($var) ? "true" :
"false";
// returns false
$var = "b00";
echo is_numeric($var) ? "true" :
"false";
?>
Page 2
Copyright 2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
Function
is_array($var)
Explanation
This function tests a variable to
see if it is a PHP associative or
numerically-indexed array.
Use this function to check if a
variable is an array, usually prior
to processing it in a loop.
Example
<?php
// returns true
$var = array("tiger", "lion",
"zebra");
echo is_array($var) ? "true" :
"false";
// returns false
$var = "zebra";
echo is_array($var) ? "true" :
"false";
?>
is_null($var)
is_object($var)
isset($var)
print_r($var)
<?php
// returns false
$var = "aa";
echo is_null($var) ? "true" :
"false";
// returns true
$var = null;
echo is_null($var) ? "true" :
"false";
?>
<?php
// returns false
$var = "exception";
echo is_object($var) ? "true" :
"false";
// returns true
$var = new Exception;
echo is_object($var) ? "true" :
"false";
?>
<?php
// returns true
$var = "yes";
echo isset($var) ? "true" :
"false";
// returns false
echo isset($test) ? "true" :
"false";
?>
<?php
$var = array("one", "two",
array("red", "green"), new
Exception, 467);
print_r($var);
?>
Page 3
Copyright 2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
Additional resources
Search and map directory trees with ease using the right PHP class
Secure your Web applications by validating user input with PHP and PEAR classes
Version history
Version: 1.0
Published: July 28, 2006
Page 4
Copyright 2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html