Complete the code to hash the password using password_hash.
<?php $password = 'mypassword'; $hash = password_hash($password, [1]); echo $hash; ?>
The password_hash function uses PASSWORD_BCRYPT to create a secure hash.
Complete the code to verify the password against the hash.
<?php $password = 'mypassword'; $hash = password_hash($password, PASSWORD_BCRYPT); if (password_verify($password, [1])) { echo 'Password is valid'; } else { echo 'Invalid password'; } ?>
The password_verify function compares the plain password with the stored hash, so the second argument must be the hash variable.
Fix the error in the code to correctly hash the password.
<?php $password = 'secret'; $hash = password_hash([1], PASSWORD_BCRYPT); echo $hash; ?>
The variable $password must be passed to password_hash to hash the actual password string.
Fill both blanks to create a dictionary of usernames and their hashed passwords, only for users with passwords longer than 6 characters.
<?php $users = ['alice' => 'secret123', 'bob' => '123', 'carol' => 'mypassword']; $hashedUsers = array_filter(array_map(function($password) { return password_hash($password, [1]); }, $users), function($password, $username) use ($users) { return strlen($users[$username]) > 6; }); print_r($hashedUsers); ?>
Use PASSWORD_BCRYPT for hashing. The filter checks password length for user 'carol' whose password is longer than 6 characters.
Fill all three blanks to create a filtered array of usernames and hashed passwords where the password length is greater than 5 and the username starts with 'a'.
<?php $users = ['alice' => 'secret123', 'bob' => '123456', 'anna' => 'mypassword']; $filteredHashed = array_filter(array_map(function($user, $password) { return [$user => password_hash($password, [1])]; }, array_keys($users), $users), function($user, $password) { return strlen($password) [2] 5 && str_starts_with($user, [3]); }, ARRAY_FILTER_USE_BOTH); print_r($filteredHashed); ?>
Use PASSWORD_BCRYPT for hashing, check if password length is greater than 5, and filter usernames starting with 'a'.