对Php函数的多次调用

对Php函数的多次调用,php,function,Php,Function,我的php脚本失败了。有什么问题吗 注意:我需要在同一页上多次调用该函数 错误: 致命错误:无法在第6行的D:\vhosts\xyz.php中重新声明countlets()(以前在D:\vhosts\xyz.php:6中声明) function testfunction($str) { $pattern = '/[a-z0-9\.]+/i'; function countLetters($matches) { return $matches[0] . '(' . strlen($mat

我的php脚本失败了。有什么问题吗

注意:我需要在同一页上多次调用该函数

错误: 致命错误:无法在第6行的D:\vhosts\xyz.php中重新声明countlets()(以前在D:\vhosts\xyz.php:6中声明)

function testfunction($str) {
 $pattern = '/[a-z0-9\.]+/i';
 function countLetters($matches) {
   return $matches[0] . '(' . strlen($matches[0]) . ')';
 }
 return preg_replace_callback($pattern, 'countLetters', $str);
}
echo testfunction("Hello world")."<br>";
echo testfunction("Life is good")."<br>";
echo testfunction("I love you")."<br>";
函数testfunction($str){
$pattern='/[a-z0-9\.]+/i';
函数计数字母($matches){
返回$matches[0].'('.strlen($matches[0]))';
}
返回preg_replace_回调($pattern,'countLetters',$str);
}
echo测试函数(“Hello world”)。“
”; 回声测试功能(“生活是美好的”)。“
”; echo testfunction(“我爱你”)。“
”;
您不应该在其他命名函数中声明命名函数。这样做意味着您将在每次调用外部函数时重新声明内部函数。将内部函数移出

function testfunction($str) {
 $pattern = '/[a-z0-9\.]+/i';

 return preg_replace_callback($pattern, 'countLetters', $str);
}

 function countLetters($matches) {
   return $matches[0] . '(' . strlen($matches[0]) . ')';
 }
echo testfunction("Hello world")."<br>";
echo testfunction("Life is good")."<br>";
echo testfunction("I love you")."<br>";
函数testfunction($str){
$pattern='/[a-z0-9\.]+/i';
返回preg_replace_回调($pattern,'countLetters',$str);
}
函数计数字母($matches){
返回$matches[0].'('.strlen($matches[0]))';
}
echo测试函数(“Hello world”)。“
”; 回声测试功能(“生活是美好的”)。“
”; echo testfunction(“我爱你”)。“
”;
不要在其他函数中声明函数。每次调用
testfunction
,它都会重新创建
countLetters
解决方案是什么?将countLetters移到testfunction之外您可以用一个示例来编辑它吗?