0

I have the following PHP code

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

I am managing to output $username, but I am not managing to output $error[] if it is a number. $error[] in my case is an array as different classes will have an error.

The current code is telling me Array Warning: Invalid argument supplied for foreach() in /web/com/140895582016925/main.php on line 22 which is for foreach($error as $result) {

4
  • 1
    You get this error because $error does not exist (btw only in the function). The return keyword break your function. return $formValue; is never reached. Commented Aug 25, 2014 at 8:42
  • It should be $e = $m -> properNames( $username ); foreach( $e as $result) ... . Commented Aug 25, 2014 at 8:43
  • what is is_numeric? is it variable or a built in function? if variable put $ sign and if funnction then is_numeric($some_variable). Commented Aug 25, 2014 at 8:43
  • please notice your wong call to if(is_numeric) { that is a function you need is_numeric($value) Commented Aug 25, 2014 at 8:43

3 Answers 3

2

The error message say it all: your $error is NOT an array.

Take a look at the is_numeric() validation part of your code.
You have an error there.
is_numeric() needs an argument.
In your case i think you need to:

if ( is_numeric($formValue ) )
{
// execute if condition
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I adjusted the code to your update. Though the $error is an array as there will be multiple classes with the $error[]. Thanks
1

try this

   <?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 Comments

@ins0 you may comment me if i am wrong. i am just updating my answer.
if was not so fast and then you deleted you answer so i couldn't :D
1

Try to use assignment:

$error = $m->properNames($username);

instead of echoing:

echo $m->properNames($username);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.