Describes some PHP functions used to determine variables.

Source: Internet
Author: User
Tags character classes

Although this feature is very easy to program using PHP, it also has an important drawback: When you need to test the type of a variable, the language with loose processing types is confusing. Fortunately, PHP developers noticed this situation and therefore included a function toolkit, especially used to test variables and find out which specific character classes they belong to-that is, whether they contain strings, integers, objects, or boolean values.

The following lists the useful functions in this category and provides instructions and application instances.

Empty ($ var)
This function is used to check whether the variable is null (no value or zero value ). Use this function to check user input, such as table variables. Ensure that they contain valid data.

Copy codeThe Code is 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", and "floating point value. Before inserting a variable into a strictly typed database domain, this function is generally used to verify whether the variable is the expected type.Copy codeThe Code is 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 the variable is a Boolean variable.

Copy codeThe Code is 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 codeThe Code is 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 (a string includes a symbol, number, and decimal point ). Before using a variable for calculation, use this function to verify whether it contains a number.

Copy codeThe Code is 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 whether it is a PHP-related or numerical ordered index array. Before processing in a loop, use this function to check whether the variable is an array.

Copy codeThe Code is 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. When evaluating the data returned by an SQL query, use this function to check whether a variable is NULL.

Copy codeThe Code is 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. Before calling a method or access attribute, this function is generally used to test whether the variable is a PHP Object.

Copy codeThe Code is 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 table submission, this function is generally used to test whether a variable is defined.

Copy codeThe Code is 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 content of a variable. This function is used to "snoop" a variable, especially when debugging a script.

Copy codeThe Code is as follows: <? Php
$ Var = array ("one", "two", array ("red", "green"), new Exception, 467 );
Print_r ($ var );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.