[英]Can I pass a reference to a variable into a function defined to take a normal variable?
[英]Can I pass a DEFINED constant to a function through a variable in PHP?
我有以下代碼:
DEFINE('DEFINEDTESTVAR', 'Hello World');
function callit($callVar) {
echo "The call is ".$callVar;
}
$passthis = 'DEFINEDTESTVAR';
callit($passthis);
我知道我可以執行callit(DEFINEDTESTVAR)
但這不是我想要的。 可能嗎?
要么傳遞常量本身:
$passthis = DEFINEDTESTVAR;
或通過constant()
訪問它,如果未定義它,則可以測試null(對於未定義的常數,按字面傳遞常數會導致產生一個具有常數名稱的字符串):
$passthis = constant('DEFINEDTESTVAR');
define('DEFINEDTESTVAR', 'Hello World'); // you should use define
function callit($callVar) {
echo "The call is ".$callVar;
}
$passthis = DEFINEDTESTVAR; // no need to use quotes
callit($passthis);
您可以使用constant()從字符串中獲取常量的值。 如果找不到命名常量,它將返回null。
$passthis = constant('DEFINEDTESTVAR');
callit($passthis);
<?php
DEFINE('DEFINEDTESTVAR', 'Hello World');
function callit($callVar) {
echo "The call is ".$callVar;
}
$passthis = DEFINEDTESTVAR;
callit($passthis);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.