1

I have a variable holding an array of errors

$errors = array();

I also have an if statement that returns whether or not a username has been entered in an input.

if(isset($_POST['submit'])) {

    if(empty($_POST['username'])) {
        echo array_push($errors, 'You did not submit a username' );
    }
}

I'm using array_push() to add an error message at the end of it. I'm using a for each loop to retrieve the values of all the error fields. although I keep getting the number of array values as well as just the intended string.... For instance it will echo out "1 You did not submit a username"

foreach($errors as $e) {
    echo $e;
    echo "<br />\n";
}

is there anyway to retrieve just the required string?

2 Answers 2

4

You have an extra echo:

if(empty($_POST['username'])) {
    /* here */ array_push($errors, 'You did not submit a username' );
}
Sign up to request clarification or add additional context in comments.

Comments

3

Remove echo from echo array_push($errors, 'You did not submit a username' );. It's not needed, and that is what's echoing the 1 in your result.

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.