我有以下PHP代码
<?php
class SimpleEmailServiceMessage
{
public function properNames($formValue) {
$formValue = strtolower($formValue); //Make all letters small case
$formValue = ucwords($formValue); //Make all first letters capital
$formValue = str_replace('','',$formValue); //Remove extra spaces
if(is_numeric($username)) {
$error[] = 'The name is invalid';
}
return $error;
return $formValue;
}
}
$username = 'john doe';
$m = new SimpleEmailServiceMessage();
echo $m->properNames($username);
foreach($error as $result) {
echo $result . '<br>';
}
?>
我正在设法输出$ username,但如果是一个数字,我无法输出$ error []。在我的情况下,$ error []是一个数组,因为不同的类会出错。
当前代码告诉我Array Warning: Invalid argument supplied for foreach() in /web/com/140895582016925/main.php on line 22
适用于foreach($error as $result) {
答案 0 :(得分:2)
错误信息全部说明:您的$ error不是数组
看一下代码中的is_numeric()验证部分
你有错误。
is_numeric()需要一个参数
在你的情况下,我认为你需要:
if ( is_numeric($formValue ) )
{
// execute if condition
}
答案 1 :(得分:1)
试试这个
<?php
class SimpleEmailServiceMessage
{
public $error;
public function properNames($formValue) {
$formValue = strtolower($formValue); //Make all letters small case
$formValue = ucwords($formValue); //Make all first letters capital
$formValue = str_replace('','',$formValue); //Remove extra spaces
if(is_numeric($formValue)) {
$this->error[] = 'The name is invalid';
}
return $formValue;
}
}
$username = 'john doe';
$m = new SimpleEmailServiceMessage();
echo $m->properNames($username);
if(isset($m->error))
{
foreach($m->error as $result) {
echo $result . '<br>';
}
}
?>
<强> Demo 强>
答案 2 :(得分:1)
尝试使用作业:
$error = $m->properNames($username);
而不是echo
ing:
echo $m->properNames($username);