Welcome Guest, Not a member yet? Register   Sign In
Weird logic error
#9

[eluser]TheFuzzy0ne[/eluser]
The default is used only if an id is not passed over as the first parameter. It's there so that you can simply check whether it's FALSE or not. If it's FALSE, you know it's not been specified by the calling function, if it's not FALSE, then it has. By unsetting the default, PHP will issue a warning if you don't specify an argument as the first parameter. This could lead to problems in the future, such as your visitors seeing errors on the page, and the application not functioning as intended. It can also lead to other errors, or other problems such invalid data being inserted into the database.

Basically, setting it with a default means that you won't get the PHP warning, but you still need to check in your function whether or not it's set to something other than FALSE before you use it.

Code:
if ($id) //if ID is not 0, "" or FALSE
{
    $cart = ($id && $quantity > 0) ? $this->add_to_cart($id, $quantity) : $this->delete_from_cart($id);
}    
else // Otherwise it's 0, "" or FALSE, and hasn't been set with a meaningful value.
{
    $cart = $this->update_cart_post();
}

If $id was not set to FALSE by default, you'd have to check if $id was set before using it, otherwise you'd end up with another PHP warning. You're application should be able to function without issuing any warnings or errors, with logging set to E_ALL. It's quite a tall order, but should be what every developer aims for. You should code carefully, and ensure that all potential errors are coded for, and handled correctly.


Messages In This Thread
Weird logic error - by El Forum - 03-07-2009, 12:54 PM
Weird logic error - by El Forum - 03-07-2009, 01:09 PM
Weird logic error - by El Forum - 03-07-2009, 01:18 PM
Weird logic error - by El Forum - 03-07-2009, 01:33 PM
Weird logic error - by El Forum - 03-07-2009, 06:29 PM
Weird logic error - by El Forum - 03-07-2009, 06:58 PM
Weird logic error - by El Forum - 03-07-2009, 07:46 PM
Weird logic error - by El Forum - 03-07-2009, 10:15 PM
Weird logic error - by El Forum - 03-07-2009, 10:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB