PHP-从类中回显数组值

PHP-从类中回显数组值,php,arrays,Php,Arrays,我有以下PHP代码 <?php class SimpleEmailServiceMessage { public function properNames($formValue) { $formValue = strtolower($formValue); //Make all letters small case $formValue = ucwords($formValue); //Make all first letters capital $form

我有以下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[]是一个数组,因为不同的类将有一个错误

当前代码告诉我,
数组警告:在第22行的/web/com/140895582016925/main.php中为foreach()提供的参数无效,
foreach($error as$result){
试试这个

   <?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>';
  }
}
?>

试试这个

   <?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>';
  }
}
?>

尝试使用分配:

$error = $m->properNames($username);
而不是
echo
ing:

echo $m->properNames($username);
尝试使用分配:

$error = $m->properNames($username);
而不是
echo
ing:

echo $m->properNames($username);

错误消息说明一切:您的$error不是数组。

请看一下代码中的is_numeric()验证部分。
您在那里有一个错误。
is_numeric()需要一个参数。
就你而言,我认为你需要:

if ( is_numeric($formValue ) )
{
// execute if condition
}

错误消息说明一切:您的$error不是数组。

请看一下代码中的is_numeric()验证部分。
您在那里有一个错误。
is_numeric()需要一个参数。
就你而言,我认为你需要:

if ( is_numeric($formValue ) )
{
// execute if condition
}

出现此错误是因为
$error
不存在(顺便说一句,仅在函数中)。关键字
return
会中断函数。
return$formValue;
从未到达。它应该是
$e=$m->properNames($username);foreach($e as$result)…
。什么是数字?它是变量还是内置函数?如果变量放$sign,如果函数是数字($some变量)。请注意您对
的调用如果(是数字){
这是一个您需要的函数
是数字($value)
您得到这个错误是因为
$error
不存在(仅在函数中)返回关键字会破坏函数。
return$formValue;
从未到达。它应该是
$e=$m->properNames($username);foreach($e as$result).
。什么是数字?它是变量还是内置函数?如果变量放$sign,如果函数是数字($some_variable)。请注意您对
的调用if(is_numeric){
这是您需要的函数
is_numeric($value)
@ins0如果我错了,你可以评论我。我只是在更新我的答案。如果不是那么快,然后你删除了你的答案,所以我无法:D@ins0如果我错了,你可以评论我。我只是在更新我的答案。如果不是那么快,然后你删除了你的答案,所以我无法:DHi,我根据你的更新调整了代码。尽管$error是一个数组由于将有多个类带有$error[]。谢谢,我根据您的更新调整了代码。尽管$error是一个数组,因为将有多个类带有$error[]。谢谢