函数未在字符串中返回变量值


function not returning variable value in a string

下面的函数工作正常,但不会只返回给$reslt:的字符串中变量$field的值

function req($slice,$field)
{
   if ($slice == "") {
      $reslt =  $field. ' cannot be empty<br />'; 
      return $reslt;
   }else{
      $reslt = "";
      return $reslt;
   }
}
req($slice,$field);
$err_mess = req();
echo $err_mess; // gives me 'cannot be empty' as result but does not show the value for the '$field' variable included in the string

此处:

req($slice,$field);
$err_mess = req();

您调用函数两次:一次是丢弃结果,第二次是不带参数。

我想你需要

$err_mess = req($slice, $field);