简体   繁体   中英

Code doesn't make sense — two conditionals within PHP try

I have inherited an application that is not doing what it's supposed to do. I have isolated the problem to the database not being properly attached. The programmer wrote this function that seemingly is suppose to evaluate whether the database is attached, calling the "attachPaymentDatabase()" function to attach it if it's not.

function attachPaymentDatabaseIfNotDoneAlready()
{
global $db;
global $hasPaymentDatabaseAttached;
// Determine if we have attached the payment tables, and if not, add them.
$hasPaymentDatabaseAttached = false;
try { 
    // this new way should work the best-- looking for PAY.
    $alldb = queryall($db, "PRAGMA database_list;");
    for ($i = 0; $i < count($alldb); $i++)
        {
        $alldb[$i] = array_change_key_case($alldb[$i], CASE_LOWER);
        if (strtolower($alldb[$i]['name']) == 'pay')
            {
            debugEmail("condition 1 worked.");
            $hasPaymentDatabaseAttached = true;
            break;
            }
        }
    // if its name changed this will also work
    if (!$hasPaymentDatabaseAttached)
        {
        $r = @$db->querySingle("SELECT * FROM PAY_PARAMETER;"); 

        $hasPaymentDatabaseAttached = true;
        debugEmail("condition 2 worked.");
        }
    } 
catch(Exception $e) 
    { 
    }
if (!$hasPaymentDatabaseAttached)
    {
    debugEmail("nothing worked.");
    attachPaymentDatabase();
    }
}

I have written a debugEmail() function that emails me a defined message with a timestamp as used above. When executing the code from the application, I can see that "condition 2 worked." is being called one second before "nothing worked.".

I don't understand how this can be. If debugEmail("condition 2 worked."); is executing, then so should too $hasPaymentDatabaseAttached = true; in which case this should not execute:

    if (!$hasPaymentDatabaseAttached)
    {
    debugEmail("nothing worked.");
    attachPaymentDatabase();
    }

But it clearly is.

What is going on here?!?!?!?

No it shouldn't, because $hasPaymentDatabaseAttached is set to true in the first condition. In still nonsense at all, but it works as described.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM