开发者

Using array_push() Help

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 retr开发者_运维技巧ieve just the required string?


You have an extra echo:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜