While this feature is easy to use in PHP programming, it also has an important flaw: when you need to test the type of a variable, the loosely processed language is confusing. Luckily, PHP developers noticed this, so they included a function toolkit that specifically tested variables and found out which particular character categories they belonged to-that is, whether they contained strings, integers, objects, or Boolean values.
The more useful functions in this category are listed below, with descriptions and application examples.
Empty ($var)
This function is used to check whether the variable is empty (no value or 0 value). Use this function to check user input, such as a table variable. Ensure that they contain valid data.
Copy Code code as follows:
<?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 the variable. For example, "string", "Integer", "Boolean", "Floating-point value", and so on. This function is used to verify that the variable is the type you expect before inserting the variable into a strictly typed database field.
Copy Code code as follows:
<?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 value (True/False). Use this function to check whether a variable is a Boolean variable.
Copy Code code as follows:
<?php
Returns True
$var = false;
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.
Copy Code code as follows:
<?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 numeric or numeric string (the string includes a symbol, a number, and a decimal point). Use this function to verify that a variable contains a number before it is used in a calculation.
Copy Code code as follows:
<?php
Returns True
$var = "+99.766";
echo is_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 an array of PHP-related or numeric sequential indices. Use this function to check whether a variable is an array before processing in a loop.
Copy Code code as follows:
<?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 check whether a variable is null when evaluating the data returned by the SQL query.
Copy Code code as follows:
<?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. This function is typically used to test whether a variable is a PHP object before calling a method or accessing the property.
Copy Code code as follows:
<?php
Returns false
$var = "Exception";
echo is_object ($var)? ' True ': ' false ';
Returns True
$var = new Exception;
echo is_object ($var)? ' True ': ' false ';
?>
Isset ($var)
This function tests a variable to see if it has been defined. When evaluating the results of a form submission, you typically use this function to test whether a variable is defined.
Copy Code code as follows:
<?php
Returns True
$var = "Yes";
echo isset ($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 "pry" a variable, especially if you are debugging a script.
Copy Code code as follows:
<?php
$var = Array ("One", "two", Array ("Red", "green"), new Exception, 467);
Print_r ($var);
?>