Checking input
Shape first, then action.
A check answers one question: can we work with this data. An empty name after trim cannot be accepted. An age below 18 cannot be accepted in this task. The answer is no or yes, without half an HTML page.
Keep the order of checks boring: first what makes reading the other fields pointless. Do not collect a list of ten errors when the lesson is about one boundary. A form in a service needs a list of errors; here one decision is enough.
trim removes spaces at the edges. A name made only of spaces is empty. Do not compare with a space by hand. In this task the case of the name does not matter for a rejection: emptiness and age decide first.
The check does not write to the database and does not send an email. It is pure. When it returns yes, another place performs the action. Mix them and you cannot test a rejection without a real database. The sandbox makes this especially visible: the function gets a name and an age and returns one word, with no output and no side effect.
Practice
Why trim
A name consists of spaces. Why can't it be accepted?
- Spaces count as a surname
- After trim the name is empty
- trim increases the age
- Spaces are null
The age boundary
Is an age of 18 accepted in this task? Answer yes or no.
Accept an application
Write function accept($name, $age). Return yes only if the name is not empty after trim and the age is at least 18. Otherwise return no.