View Single Post
Old 03-20-2008, 01:34 PM   #1 (permalink)
quantumkangaroo
The Contributor
 
quantumkangaroo's Avatar
 
Join Date: Feb 2008
Location: Pretoria, South Africa
Posts: 42
Thanks: 1
quantumkangaroo is an unknown quantity at this point
Default Easy Peasy Variable Validation

Doing validation whether it is for security purposes or just validating user input is very easy without using regex.

Lets take a look at our variables

PHP Code:
$integer 12345;
$float 123.45;
$string 'this is a string';
$null NULL;
$bool TRUE;
$array = array('Blue''Purple''Green'); 
For an explanation on each variable this post should help TalkPHP - Variables for Beginners

Now lets validate our variables, we'll start by validating our variables with is_numeric()

PHP Code:
is_numeric($integer); // Returns True
is_numeric($float); // Returns True
is_numeric($string); // Returns False
is_numeric($null); // Returns False
is_numeric($bool); // Returns False
is_numeric($array); // Returns False 
As you can see is_numeric() checks if the value of the variable is numeric. Now lets move on to is_float()

PHP Code:
is_float($integer); // Returns False
is_float($float); // Returns True
is_float($string); // Returns False
is_float($null); // Returns False
is_float($bool); // Returns False
is_float($array); // Returns False 
This checks to see if the variable is a valid float, isDecimalNumber() could also be used for this kind of validation. Moving on to is_string()

PHP Code:
is_string($integer); // Returns False
is_string($float); // Returns False
is_string($string); // Returns True
is_string($null); // Returns False
is_string($bool); // Returns False
is_string($array); // Returns True 
is_string() checks to see if the variables are valid strings, these variables would only be valid strings if they had quotes '' or "". Lets see what happens with is_null()

PHP Code:
is_null($integer); // Returns False
is_null($float); // Returns False
is_null($string); // Returns False
is_null($null); // Returns True
is_null($bool); // Returns False
is_null($array); // Returns False 
It checks to see if the value of the specified variable is NULL if it is it returns True if not it returns False. Now were going to look at is_bool()

PHP Code:
is_bool($integer); // Returns False
is_bool($float); // Returns False
is_bool($string); // Returns False
is_bool($null); // Returns False
is_bool($bool); // Returns True
is_bool($array); // Returns False 
Last but not least here is is_array()

PHP Code:
is_array($integer); // Returns False
is_array($float); // Returns False
is_array($string); // Returns False
is_array($null); // Returns False
is_array($bool); // Returns False
is_array($array); // Returns True 
Here it checks to see if the given variable is a valid array. Well that about wraps it up for variable validation, there are more functions to use when it comes to validation but we will go into those later.
__________________
virtueCart v1.0.5 developed by WebDevSA


Last edited by quantumkangaroo : 03-20-2008 at 03:08 PM.
Send a message via MSN to quantumkangaroo Send a message via Skype™ to quantumkangaroo
quantumkangaroo is offline  
Reply With Quote