Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Integrating Validation Errors Escaping Output

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

Array for displaying multiple message error

So I am tyring to do the extra credit for this course. I am having trouble figuring out how to have the array hold all the values of the errors. What I did and got stuck was the following:

$error_message = array(); $error_message = array($error_message); // Here I am trying to hold the error message in a variable, since all I can reference from the excersices are text values in the arrays Im confused.

Then under the else statement before the form I run a loop:

foreach ($error_message as $error) { // Here I guess at least I would see the index number echo but I see nothing on the form when I run it. echo $error; }

Philip Cox
Philip Cox
14,818 Points

Randy goes into great detail later in the course processes that resemble what you'r trying to achieve.

2 Answers

Philip Cox
Philip Cox
14,818 Points

Hello. You can't have an array called $error_message, if you'r receiving a value and also holding it in $error_message. Otherwise it would be like looping through an empty array, and they would conflict. Give the array a different name. Loop through all you error messages and put them into $error, then I believe the syntax is, $error_message [ ] = $error; This places all the $error values into the array. Then return the array if it's a function.

Hope this helps :)

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

Im sorry Im a little confused with your explanation.

Yes, I hold all the errors under a variable called: $error_message.

So are you saying:

$errors = array(); $errors = $error_message [ ];

then

foreach($errors as $error { echo $error; }

Philip Cox
Philip Cox
14,818 Points

```php $errors = array( ); foreach $error_message as $error { $errors [ ] = $error; } return $errors;