Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get user input from a POST request safely.
PHP
<?php
$user_input = $_POST[[1]];
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the key causes a syntax error.
Using a wrong key name returns null.
✗ Incorrect
The key to access POST data must be a string in quotes, like "username".
2fill in blank
mediumComplete the code to sanitize the user input as a string.
PHP
<?php
$clean_input = filter_var($user_input, [1]);
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using validation filters instead of sanitization filters.
Choosing filters meant for emails or numbers.
✗ Incorrect
FILTER_SANITIZE_STRING removes unwanted characters from strings.
3fill in blank
hardFix the error in the code to validate if input is an integer.
PHP
<?php if (filter_var($input, [1]) === false) { echo "Invalid number."; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using sanitization filters instead of validation filters.
Confusing boolean validation with integer validation.
✗ Incorrect
FILTER_VALIDATE_INT checks if the input is a valid integer.
4fill in blank
hardFill both blanks to sanitize and then validate an email input.
PHP
<?php $email = filter_var($raw_email, [1]); if (filter_var($email, [2]) === false) { echo "Invalid email."; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Validating before sanitizing.
Using wrong filters for email validation.
✗ Incorrect
First sanitize the email, then validate it.
5fill in blank
hardFill all three blanks to create a sanitized array of usernames longer than 3 characters.
PHP
<?php $usernames = ['alice', 'bob', 'eve123', 'jo']; $filtered = array_filter(array_map(function($name) { return filter_var($name, [1]); }, $usernames), function($name) { return strlen($name) [2] [3]; }); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using validation filters instead of sanitization.
Using wrong comparison operators or values.
✗ Incorrect
Sanitize each username, then keep only those longer than 3 characters.