zoukankan      html  css  js  c++  java
  • PHP 判断常量,变量和函数是否存在

    判断变量是否被定义:defined()

    1 if (defined('CONST_NAME')) {
    2     //do something 
    3 }

    判断变量是否存在:isset() ,注意变量未声明或声明时赋值为NULL,isset均返回FALSE,如:

    1 f (isset($var_name)) {
    2     //do something
    3 }

    函数检测用function_exists,注意待检测的函数名也需要使用引号,如:

    1 if (function_exists('fun_name')) {
    2  fun_name();
    3 }
    4  

    实例:

    <?php 
    /* 判断常量是否存在*/ 
    if (defined('MYCONSTANT')) { 
    echo MYCONSTANT; 
    } 
    //判断变量是否存在 
    if (isset($myvar)) { 
    echo "存在变量$myvar."; 
    } 
    //判断函数是否存在 
    if (function_exists('imap_open')) { 
    echo "存在函数imag_openn"; 
    } else { 
    echo "函数imag_open不存在n"; 
    } 
    ?>
     

    function_exists判断函数是否存在

    1 <?php
    2 if (function_exists('test_func')) {
    3     echo "函数test_func存在";
    4 } else {
    5     echo "函数test_func不存在";
    6 }
    7 ?>

    filter_has_var函数

    filter_has_var() 函数检查是否存在指定输入类型的变量。
    若成功,则返回 true,否则返回 false。

     1 <?php
     2 if(!filter_has_var(INPUT_GET, "name"))
     3  {
     4  echo("Input type does not exist");
     5  }
     6 else
     7  {
     8  echo("Input type exists");
     9  }
    10 ?>  

    输出为. Input type exists

  • 相关阅读:
    LintCode-Search for a Range
    LintCode-Serialization and Deserialization Of Binary Tree
    LeetCode-Reverse Words in a String
    LeetCode-Reorder List
    LeetCode-Word Break
    LeetCode-Word Ladder
    LeetCode-Valid Palindrome
    cf div2 237 D
    POJ 1759
    cf div2 238 D
  • 原文地址:https://www.cnblogs.com/zsczsc/p/7089732.html
Copyright © 2011-2022 走看看