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 Build a Basic PHP Website (2018) Enhancing a Form Setting an Error Message Variable

Seth Johnson
Seth Johnson
15,200 Points

How does the one $error_message work for all "if" statements?

I'm struggling with a lot of what's happening in this video segment, but the main thing is:

How does the single $error_message variable generated by Alena in this section work for all of the different "if" conditionals?

My understanding is/was that there'd have to be unique variable names for each respective conditional; unless there's an exception here somewhere that I missed?

Is the answer simply that all of the previously-mentioned "if" conditionals are contained within the parent "if" conditional that checks/validates the request method near the beginning of the code?

Any advice/input would be greatly appreciated. :)

$error_message is a variable and it is set when one of the steps fail.

It seems that each step is expected to work in order to continue. If one of these steps fail then $error_message is set with the message at that point.

It will be undefined the whole way through until it is set. Once it is set the app will stop and the message will show.

Multiple steps can't fail at once in this set up. It will fail at the first instance of an error. Once that is fixed and re-ran then it will fail at the second instance of an error... until eventually there are no errors.

Therefore it doesn't clash / get overwritten like you are probably thinking would happen.

1 Answer

This is not an answer - I also am confused about the removal of the exit commands with each conditional. If the user did not put in the required fields, the error message variable is assigned a string. But then the code proceeds to check the other fields in sequence, so the last field with an error will populate the error message.

It seems like a waste to continue testing the fields after you know that one of the three required/mandatory fields has not been entered.