0% found this document useful (0 votes)
68 views4 pages

Mellonfire PHP Var Test Funcs

Php variables and functions

Uploaded by

parama777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views4 pages

Mellonfire PHP Var Test Funcs

Php variables and functions

Uploaded by

parama777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Determine class type with these 10 PHP

variable testing functions

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

Determine class type with these 10 PHP variable testing functions

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)

This function returns the type of a


variable - for example, "string",
"integer", "Boolean", "float" etc.
Use this function to verify that
variables are of the type you
expect, usually before inserting
them into a strictly-typed database
field.

is_bool($var)

This function tests a variable to


see if it contains a Boolean
(true/false) value
Use this function to check if a
variable is a Boolean variable.

is_string($var)

This function tests a variable to


see if it is a string.
Use this function to check if a
variable holds string data.

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)

This function tests a variable to


see if it contains a number or
numeric string (strings containing
a sign, numbers and decimal
points).

<?php
// returns true
$var = "+99.766";
echo is_numeric($var) ? "true" :
"false";

Use this function to verify that a


variable contains a number,
usually before using it in a
calculation.

// 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

Determine class type with these 10 PHP variable testing functions

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)

This function tests a variable to


see if it is NULL.
Use this function to verify if a
variable is NULL or not, usually
when evaluating data returned by
an SQL query.

is_object($var)

This function tests a variable to


see if it is a PHP object.
Use this function to test if a
variable is a PHP object, usually
before calling a method or
accessing a property.

isset($var)

This function tests a variable to


see if it has already been defined.
Use this function to test if a
variable has been defined, usually
when evaluating the results of a
form submission.

print_r($var)

This function prints the contents of


a variable.
Use this function to "look inside" a
variable, typically when debugging
a script.

<?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

Determine class type with these 10 PHP variable testing functions

Additional resources

TechRepublic's Downloads RSS Feed


Sign up for TechRepublic's Downloads Weekly Update newsletter
Sign up for TechRepublic's [TOPIC] newsletter
Check out all of TechRepublic's free newsletters

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

Tell us what you think


TechRepublic downloads are designed to help you get your job done as painlessly and effectively as possible.
Because we're continually looking for ways to improve the usefulness of these tools, we need your feedback.
Please take a minute to drop us a line and tell us how well this download worked for you and offer your
suggestions for improvement.
Thanks!
The TechRepublic Downloads Team

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

You might also like