PHP Build a Basic PHP Website (2018) Enhancing a Form Setting an Error Message Variable

Seth Johnson
Seth Johnson
14,877 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. :)

Alexander Mackenzie
Alexander Mackenzie
10,393 Points

$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.