I was wondering if the following program flow will prevent the finally from being run in this try-catch-finally block due to a return in the try.
Please excuse poor error checking and sanitisation, this is just a mock:
function doLogin() {
$dbh = new PDO('mysql:host=localhost;dbname=test', "root", "");
$errors = array();
$loginSuccess = false;
try {
$query = $dbh->prepare('SELECT *
FROM users
WHERE username = :username');
$query->bindParam(':username', $_POST['username'], PDO::PARAM_STR);
$result = $query->fetch(PDO::FETCH_ASSOC);
if (!password_verify($_POST['password'], $result['password'])) {
array_push($errors, 'Invalid password.');
return; // will this return prevent the finally from being run?
}
else {
$loginSuccess = true;
}
}
catch (PDOException $pdoEx) {
echo 'Error occurred ' . $pdoEx->getMessage();
}
finally {
$dbh = null;
}
}
Code is pretty poorly written, but hopefully you understand my point.