我想在关联数组中管理一组文本转换。
该示例有效,但会产生通知。当规则在与定义数组的文件不同的文件中评估时,它将不起作用。我怎样才能解决这个问题?
代码
<?php
function noop($x){
return $x;
}
function trimAndUpper($x){
return strtoupper(trim($x));
}
$a = array(
" a " => trim,
" b " => noop,
" c " => trimAndUpper,
);
foreach($a as $key=>$func){
echo "key:'$key' value:'{$func($key)}'\n";
}
输出
❯❯❯ php ./experiment.php
PHP Notice: Use of undefined constant trim - assumed 'trim' in /tback/src/experiment.php on line 12
Notice: Use of undefined constant trim - assumed 'trim' in /tback/src/experiment.php on line 12
PHP Notice: Use of undefined constant noop - assumed 'noop' in /tback/src/experiment.php on line 13
Notice: Use of undefined constant noop - assumed 'noop' in /tback/src/experiment.php on line 13
PHP Notice: Use of undefined constant trimAndUpper - assumed 'trimAndUpper' in /tback/src/experiment.php on line 14
Notice: Use of undefined constant trimAndUpper - assumed 'trimAndUpper' in /tback/src/experiment.php on line 14
key:' a ' value:'a'
key:' b ' value:' b '
key:' c ' value:'C'
php 版本是 PHP 5.3.27,我现在必须与 5.3 保持兼容。