Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用自定义函数在PHP中格式化字符串_Php_String_Function - Fatal编程技术网

无法使用自定义函数在PHP中格式化字符串

无法使用自定义函数在PHP中格式化字符串,php,string,function,Php,String,Function,您需要将所有操作的结果指定给即将返回的变量 <?php $string = 'tHis is aN unEVen string that needs to be formated properly'; // custom function created combining multiple functions function varform($var){ ucwords(strtolower(htlmentities(trim($var)))); return $var;

您需要将所有操作的结果指定给即将返回的变量

<?php
$string = 'tHis is aN unEVen string that needs to be formated properly';

// custom function created combining multiple functions
function varform($var){
   ucwords(strtolower(htlmentities(trim($var))));
   return $var;
}

$string = varform($string);
echo $string;
?>

1用htmlentities替换htlmentities

正如前面的评论所说

<?php
$string = 'tHis is aN unEVen string that needs to be formated properly';

// custom function created combining multiple functions
function varform($var){
   // assign change to the variable
   $var = ucwords(strtolower(htmlentities(trim($var))));
   return $var;
}

$string = varform($string);
echo $string;
?>

您的代码有两个问题

function varform($var){
  return ucwords(strtolower(htmlentities(trim($var))));
}
您需要从PHP方法中获取返回值,并从函数中返回该值

function varform($var){
   ucwords(strtolower(htlmentities(trim($var))));
------------------------^ //It's htmlentities() not htlmentities()
   return $var; //you're just returning the value that is passed to the method
}

欢迎使用SO,请使用提供的格式化工具格式化您的问题。这次我帮你把它清理干净了。htlmentities就是htmlentities你打错了mistake@johnP-谢谢,我现在也把它修好了
function varform($var){
   $var = ucwords(strtolower(htmlentities(trim($var))));
   return $var;
}