Home:ALL Converter>Strange behaviour of Try

Strange behaviour of Try

Ask Time:2014-03-05T01:53:42         Author:ProgZi

Json Formatter

I was experimenting with try catch and found some strange behaviour.

The code

<?php
$user = "WrongUserName";
$pass = "WrongPassword";

try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    echo "Success";
}
catch (PDOException $e) {
    echo "Login Failed";
}
?>

This outputs "Login Failed" as expected, but when I delete variables

<?php
// Variables for $dbh is missing

try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    echo "Success";
}
    catch (PDOException $e) {
    echo "Login Failed";
}
?>

It outputs

Notice: Undefined variable: user in C:\xampp\htdocs\simpleBD\db.php on line 6 Notice: Undefined variable: pass in C:\xampp\htdocs\simpleBD\db.php on line 6 Success

WHY does is output the "Success" instead of "Login Failed"

Author:ProgZi,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/22179356/strange-behaviour-of-try
yy