第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

PHP 在驗(yàn)證之前檢測(cè)它是 CONSTANT 還是 STRING

PHP 在驗(yàn)證之前檢測(cè)它是 CONSTANT 還是 STRING

PHP
呼啦一陣風(fēng) 2023-11-03 16:03:40
您可以通過以下方式執(zhí)行此操作:function count_array_values($my_array, $match) {    $count = 0;     foreach ($my_array as $key => $value)     {         if ($value == $match)         {             $count++;         }     }        return $count; } $array = ["hello","bye","hello","John","bye","hello"];$output =[];foreach($array as $a){    $output[$a] = count_array_values($array, $a); }arsort($output);print_r($output);你會(huì)得到類似的輸出Array ( [hello] => 3 [bye] => 2 [John] => 1 )我正在開發(fā)一個(gè)驗(yàn)證變量和常量的函數(shù)。function checkVars($var){ if(isConstant($var)){ <--here is my problem, how do I check if its a Constant and or a string?  return defined($var); }else{  //use other functions to check if its a valid variable }}有沒有辦法檢查它是字符串還是常量?編輯: 這背后的想法是節(jié)省重復(fù)任務(wù)的代碼行,例如:if(defined('CONS')){ if(CONS > 0 && CONS !== false){  //and so on.. }}這只是一個(gè)示例,但您可以使用 4 行代碼來驗(yàn)證常量或變量以滿足特定應(yīng)用程序的需求。這個(gè)想法是通過返回 true 或 false 的單個(gè)函數(shù)調(diào)用來保存所有工作:if(isValid('CONS')){ //do stuff on true}else{ //do stuff on false}
查看完整描述

3 回答

?
慕哥9229398

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊

檢查一個(gè)常量是否由她的名字定義:


defined('CONSTANT');

檢查是否有任何值是字符串:


is_string(CONSTANT);

從邏輯上講,如果它不是常量,那么它只是一個(gè)字符串。


要檢查是否是常量,您必須在字符串中傳遞常量名稱。您還可以使用 檢查是否存在具有相同值的常量get_defined_constants(),但您不會(huì)知道是否是相同的常量。


define('MYCONST', "THE VALUE");

function exists_a_constant($value)

{

    $constants = get_defined_constants(true);

    return in_array($value, $constants['user']); // true if finds or false if not

    // return array_search($value, $constants['user']);    //Will return the key (name of the constant)

}


function checkVars($var)

{

    if (exists_a_constant($var)) {

        echo "exists a constant";

    } else {

        echo "not";

    }

}


checkVars(MYCONST);

// exists a constant

checkVars('MYCONST');

// not

checkVars("THE VALUE");

// exists a constant

checkVars("random string");

// not


查看完整回答
反對(duì) 回復(fù) 2023-11-03
?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

您將使用defined()來測(cè)試該常量是否存在并已定義。然后使用is_string()withconstant()來判斷常量是否是字符串。我假設(shè)您正在使用,return因?yàn)榇藯l件是函數(shù)的一部分:


if(defined($constantName) and is_string(constant($constantName))) {

? ? return constant($constantName);

} else {

? ? // other code

}

運(yùn)行以下測(cè)試我可以看到返回了“bar”:


define("FOO", "bar");

$constantName = "FOO";


if(defined($constantName) and is_string(constant($constantName))) {

? ? echo constant($constantName); // 'bar'

} else {

? ? // other code

}


查看完整回答
反對(duì) 回復(fù) 2023-11-03
?
繁星淼淼

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊

我做了一些更改并且有效。


get_define_constants 獲取一個(gè)包含所有已定義常量的數(shù)組,然后我使用 array_key_exists 來檢查您傳遞的常量是否在數(shù)組中。


define('FOO','bar');

function exists_a_constant($value){

? ? $constants = get_defined_constants();

? ? return array_key_exists($value,$constants);?

}


if(exists_a_constant('FOO')){

? ? echo 'defined';

}else{

? ? echo 'not defined';

}


查看完整回答
反對(duì) 回復(fù) 2023-11-03
  • 3 回答
  • 0 關(guān)注
  • 201 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)