PHP Common judgment

Source: Internet
Author: User
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?

1

2 Returns false

3 $var = "Hello";

4 echo Empty ($var)? "True": "false";

5 Returns True

6 $var = 0000;

7 echo Empty ($var)? "True": "false";

8 ?>

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?

1

2 Returns string

3 $var = "Hello";

4 Echo GetType ($var);

5 Returns double

6 $var = 1000.56;

7 Echo GetType ($var);

8 ?>

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?

1

2 Returns True

3 $var = false;

4 echo Is_bool ($var)? "True": "false";

5 ?>

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?

1

2 Returns True

3 $var = "Exception";

4 echo is_string ($var)? "True": "false";

5 Returns True

6 $var = "88408";

7 echo is_string ($var)? "True": "false";

8 ?>

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?

1

2 Returns True

3 $var = "+99.766";

4 echo is_numeric ($var)? "True": "false";

5 Returns false

6 $var = "B00";

7 echo is_numeric ($var)? "True": "false";

8 ?>

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?

1

2 Returns True

3 $var = Array ("Tiger", "lion", "zebra");

4 echo Is_array ($var)? "True": "false";

5 Returns false

6 $var = "Zebra";

7 echo Is_array ($var)? "True": "false";

8 ?>

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?

1

2 Returns false

3 $var = "AA";

4 echo is_null ($var)? "True": "false";

5 Returns True

6 $var = null;

7 echo is_null ($var)? "True": "false";

8 ?>

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?

1

2 Returns false

3 $var = "Exception";

4 echo is_object ($var)? "True": "false";

5 Returns True

6 $var = new Exception;

7 echo is_object ($var)? "True": "false";

8 ?>

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?

1

2 Returns True

3 $var = "Yes";

4 echo isset ($var)? "True": "false";

5 Returns false

6 echo isset ($test)? "True": "false";

7 ?>

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?

1

2 $var = Array ("One", "one", "I", Array ("Red", "green"), new Exception, 467);

3 Print_r ($var);

4 ?>
  • 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.