Introduce some function _php techniques of PHP judgment variables

Source: Internet
Author: User
Tags numeric php programming
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);
?>

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.