PHP variable state function and usage

Source: Internet
Author: User
PHP has several functions that can be used to test the state of a variable. The first function is isset (). It has the following function prototype: boolisset (mixedvar); [; mixedvar [,...]). This function requires a variable name as the parameter.

PHP has several functions that can be used to test the state of a variable. The first function is isset (). It has the following function prototype: bool isset (mixed var); [; mixed var [,...]), this function requires a variable name as a parameter. if the variable exists, true is returned; otherwise, false is returned. You can also pass a list of variables separated by commas. If all variables are set, the isset () function returns true. You can also use the unset () function corresponding to the isset () function to destroy a variable. It has the following function prototype:
Void unset (mixed var); [; mixed var [,...])

This function will destroy a passed variable. The empty () function can be used to check whether a variable exists and whether its value is non-null or non-zero. the corresponding return value is true or false. It has the following function prototype:
Bool empty (mixed var );

Now we can look at an example of using these three functions. Try to temporarily add the following code to a script:
Echo 'isset ($ username): '. isset ($ username ).'
';
Echo 'isset ($ nothere): '. isset ($ nothere ).'
';
Echo 'empty ($ username): '. empty ($ username ).'
';
Echo 'empty ($ nothere): '. empty ($ nothere ).'
';

Refresh the page to view the running results. No matter what value is entered in the form field or no value is input at all, the $ username variable in the isset () function returns 1 (true ). In the empty () function, the returned value depends on the value entered in the form field. $ Nothere variable does not exist. Therefore, a blank result (false) will be generated in the isset () function, and 1 (true) will be generated in the empty () function ).
These functions are very convenient to use, so that you can enter the form correctly.

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.