简体   繁体   English

如何检查常量是否在函数内部定义-PHP

[英]How to check if constant is defined, inside function - php

I am trying to make a small PHP function which can check if a constant is defined, and if so, echo it, and if not, echo space or nothing. 我正在尝试制作一个小的PHP函数,该函数可以检查是否定义了常量,如果定义了常量,则回显它,如果没有,则回显空格或什么也没有。

Right now, the if(defined() part is not working, because the constant is being transferred to a variable inside the function. 现在, if(defined()部分不起作用,因为常量正在被传递到函数内部的变量中。

function getConstant($constant) {  
  if(defined($constant)) {
    echo constant($constant);
  } else {
    echo '';
  }
}

The echo constant($constant) part is working fine, but I cannot check if the constant is actually defined because it is a variable now. echo constant($constant)部分工作正常,但是我无法检查该常量是否已实际定义,因为它现在是一个变量。

I cannot seem to find a solution for it 我似乎找不到解决方案

public static function isConstants($constant) {
    $oClass = new ReflectionClass(__CLASS__);
    $allConstants = $oClass->getConstants();
    if (isset($allConstants[$constant])) {
       echo $allConstants[$constant];
    } else {
       echo '';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM