Determine class type with these 10 PHP variable testing functions
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.
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.
Table A
|
Function |
Explanation |
Example |
| empty($var) |
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. |
<?php // returns false $var = "hello"; echo empty($var) ? "true" : "false"; // returns true $var = 0000; echo empty($var) ? "true" : "false"; ?> |
| 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. |
<?php // returns string $var = "hello"; echo gettype($var); // returns double $var = 1000.56; echo gettype($var); ?> |
| 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. |
<?php // returns true $var = false; echo is_bool($var) ? "true" : "false"; ?> |
| 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. |
<?php // returns true $var = "exception"; echois_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). Use this function to verify that a variable contains a number, usually before using it in a calculation. |
<?php // returns true $var = "+99.766"; echois_numeric($var) ? "true" :"false"; // returns false $var = "b00"; echo is_numeric($var) ?"true":"false"; ?> |
| is_array($var) |
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. |
<?php // returns true $var = array("tiger", "lion","zebra"); echois_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. |
<?php // returns false $var = "aa"; echo is_null($var) ? "true" : "false"; // returns true $var = null; echo is_null($var) ? "true" : "false"; ?> |
| 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. |
<?php // returns false $var = "exception"; echo is_object($var) ? "true" : "false"; // returns true $var = new Exception; echois_object($var) ? "true" : "false"; ?> |
| 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. |
<?php // returns true $var = "yes"; echoisset($var) ? "true" : "false"; // returns false echo isset($test) ? "true" : "false"; ?> |
| 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 $var = array("one", "two", array("red", "green"), new Exception, 467); print_r($var); ?> |
没有评论▼