Introduce some functions of PHP judgment variables
2010-07-31
Unlike other languages, PHP is not a strict type language. Basically, this means that the developer does not have to explicitly set the type of the variable (number, String, Boolean) before using a variable. Instead, the PHP interpreter automatically detects the type of the variable based on the information stored in the variable.
While this feature is easy to use with PHP, it also has an important flaw: when you need to test the type of a variable, the loosely-handled language can be confusing. Fortunately, PHP developers are aware of this, so there is a function toolkit in it, especially to test variables and find out which particular character category they belong to. That is, whether they contain a string, an integer, an object, or a Boolean value.
The more useful functions in this category are listed below, with descriptions and application examples.
Empty ($var)
This function is used to check if the variable is empty (no value or 0 value). Use this function to check user input, for example, a table variable. Ensure that they contain valid data.
View Source print?
| 4 |
echo Empty ($var)? "True": "false"; |
| 7 |
echo Empty ($var)? "True": "false"; |
GetType ($var)
This function returns the type of the variable. For example, "string", "Integer", "Boolean", "floating-point value", and so on. This function is commonly used to verify that a variable is the type you expect before inserting a variable into a strictly typed database field.
View Source print?
Is_bool ($var)
This function tests a variable to see if it contains a Boolean value (True/False). Use this function to check if a variable is a Boolean variable.
View Source print?
| 4 |
echo Is_bool ($var)? "True": "false"; |
Is_string ($var)
This function tests whether a variable is a string variable. Use this function to check whether a variable contains string data.
View Source print?
| 4 |
echo is_string ($var)? "True": "false"; |
| 7 |
echo is_string ($var)? "True": "false"; |
Is_numeric ($var)
This function tests a variable to see if it contains a numeric or numeric string (the string consists of a symbol, a number, and a decimal point). Use this function to verify that a variable contains a number before it is used for evaluation.
View Source print?
| 4 |
echo is_numeric ($var)? "True": "false"; |
| 7 |
echo is_numeric ($var)? "True": "false"; |
Is_array ($var)
This function tests a variable to see if it is an array of PHP-related or numeric sequential indexes. Use this function to check whether a variable is an array before processing in a loop.
View Source print?
| 3 |
$var = Array ("Tiger", "lion", "zebra"); |
| 4 |
echo Is_array ($var)? "True": "false"; |
| 7 |
echo Is_array ($var)? "True": "false"; |
Is_null ($var)
This function tests a variable to see if it is null. Use this function to check if a variable is null when evaluating the data returned by the SQL query.
View Source print?
| 4 |
echo is_null ($var)? "True": "false"; |
| 7 |
echo is_null ($var)? "True": "false"; |
Is_object ($var)
This function tests a variable to see if it is a PHP object. This function is typically used to test whether a variable is a PHP object before invoking a method or accessing a property.
View Source print?
| 4 |
echo is_object ($var)? "True": "false"; |
| 7 |
echo is_object ($var)? "True": "false"; |
Isset ($var)
This function tests a variable to see if it has been defined. This function is typically used to test whether a variable is defined when evaluating the results of a table submission.
View Source print?
| 4 |
echo isset ($var)? "True": "false"; |
| 6 |
echo isset ($test)? "True": "false"; |
Print_r ($var)
This function prints the contents of a variable. Use this function to "spy" on a variable, especially when debugging a script.
View Source print?
| 2 |
$var = Array ("One", "one", "I", Array ("Red", "green"), new Exception, 467); |