Php obtains the variable type and determines whether it is a variable.

Source: Internet
Author: User
In php, variables do not need to be defined in advance, so in many cases, the php variable type is ambiguous, but php provides the gettype () function to obtain the variable type, it can determine all the variable types in php.

In php, variables do not need to be defined in advance, so in many cases, the php variable type is ambiguous, but php provides the gettype () function to obtain the variable type, he can determine all the variable types in php.

Variable types

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.

The instance code is as follows:

  1. // Returns string
  2. $ Var = "hello ";
  3. Echo gettype ($ var );
  4. // Returns double
  5. $ Var = 1000.56;
  6. Echo gettype ($ var );
  7. ?>

Warning

Do not use gettype () to test a certain type, because the returned string may need to be changed in future versions. In addition, because it contains a string comparison, it runs slowly.

Use the is _ * function instead.

Check valid variables

Isset is used to determine whether a variable exists or not. If yes, true is returned. otherwise, false is returned. this variable is very useful for us. we can determine whether a variable such as get post is present.

Isset instance"

The instance code is as follows:

  1. $ A = "test ";
  2. $ B = "anothertest ";
  3. Echo isset ($ )? 'True': 'false ';

Output result: true

The instance code is as follows:

  1. // Returns true
  2. $ Var = "yes ";
  3. Echo isset ($ var )? "True": "false ";
  4. // Returns false
  5. Echo isset ($ test )? "True": "false ";
  6. ?>

The above are two super simple php functions, isset () and gettype (), which are no difficulty. For more information, see.

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.