PHP How to determine constants, variables and functions exist in the instance code of the detailed

Source: Internet
Author: User
If you understand the above sentence, then the next is nonsense, the PHP manual is written in a very full. In a word, I solved all the problems in my title.

The code is as follows:

if (defined (' Const_name ')) {    //do something}

Variable detection is the use of isset, note that variables are not declared or declared when the assignment value is Null,isset return false, such as:

if (Isset ($var _name)) {    //do something}

function detection with function_exists, note that the function name to be detected also needs to use quotation marks, such as:

if (function_exists (' Fun_name ')) {fun_name ();}

Let's just say we're going to see an example.

<?php/* Determines if the constant exists */if (defined (' myconstant ')) {echo myconstant;}//Determines if the variable exists if (Isset ($myvar)) {echo "exists variable $myvar." ; }//Determine if the function exists if (function_exists (' Imap_open ')) {echo "exists function Imag_openn";} else {echo "function Imag_open does not exist n";}?>

Function_exists determine if a function exists

<?phpif (function_exists (' Test_func ') {    echo "function Test_func exists";} else {    echo "function Test_func does not exist";}? >

Filter_has_var function

The Filter_has_var () function checks for the existence of a variable of the specified input type.
Returns true if successful, otherwise false is returned.

<?phpif (!filter_has_var (input_get, "name") {Echo ("INPUT type does not exist");} else {echo ("Input type exists");}? >

Output is. Input type exists

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.