views:

293

answers:

6

Suppose we have following function in PHP:

public function testSomething()
{
    $name = perform_sql_query("SELECT name FROM table WHERE id = $entity_id;");
    assert($name == "some_name");
}

Query is syntactically correct, but since $entity_id is undefined - query will always search for 'id = 0', which is semantically incorrect.

I would like such functions to fail automatically, when they try to use undefined variable. Is there such mechanism in PHP? Or maybe there is some tool, that can be used to analyze PHP source code to find such cases?

UPDATE Those undefined variables can occur anywhere in project, so correct decision will be to check function arguments in every function.

UPDATE2 Setting error handler helped. Now anytime uninitialized variable is used - exception is thrown.

+6  A: 

PHP has several severity levels in script errors. You can set what you want it to consider an error with the function error_reporting() or the php.ini-directive with the same name. This is how you set PHP to report anything which might lead to errors (this can lead to quite a lot of messages, if you are setting it for the first time, but fixing these errors will give you a much more stable application):

# In your php script:
error_reporting(E_ALL | E_STRICT);

# Or in your php.ini:
error_reporting = E_ALL | E_STRICT
soulmerge
Someone else may want to change error reporting for other reasons and break something.
slipbull
If the program is verified to be correct, what's there to break? On production site, setting error reporting to on is a must. You could turn it off for live site, but then see my answer.
Extrakun
Someone else shouldn't touch the error_reporting level. Or revert it back to its original value, if he *really* must.
soulmerge
Not always. As the accepted answer sais, sometimes you want error reporting off to fail nicely in production sites.
slipbull
No, you don't need to turn error_reporting off, if you have a custom error handler. The error_handler will *always* be called, regardless of current error_reporting level. As the manual states: 'It is important to remember that the standard PHP error handler is completely bypassed.' You should have a look at error handlers in PHP: http://php.net/manual/en/function.set-error-handler.php
soulmerge
Looks good. If only you had stated that from the beginning...
slipbull
+2  A: 

If you turn on all warning and notices, evaluating that string should throw you an error message.

Also, it will evaluate to invalid SQL (it will be id= not id=0), so your perform_sql_query should be reporting that also.

Paul Dixon
A: 

Use the isset() function before accesing variables.

slipbull
it could be set and yet still have a empty value
Andi
then use empty()
slipbull
-1 The question was not about how to fix the error, it was about how to get PHP to report it.
soulmerge
getting php to report it is easier, but not safe
slipbull
Yes, but the question is: 'How do I *find* the error', not 'How do I *fix* the error'
soulmerge
+2  A: 

I normally check if my values are null within my function. You can use isset() and also if its a empty string using something like below:

public function testSomething()
{    
    if(!empty($entity_id)){
            $name = perform_sql_query("SELECT name FROM table WHERE id = $entity_id;");    
            assert($name == "some_name");
            return true;
    }
    return false;
}

I would also set the error reporting using the below line:

error_reporting(E_ALL | E_STRICT);
Andi
use empty() to check for unset or empty variables, it's shorter and more efficient than isset() and !=""
xav0989
cheers for that didnt know that. Thought it would raise an error if unset :) Cheers
Andi
+1  A: 
  • Set error reporting to E_ALL in your php apps by using:

    error_reporting( E_ALL );

Or by setting appropriate values for error_reporting variable in php.ini or .htaccess. This allows you to catch all errors including those related to undefined variables.

  • Set up the php error_log in your development environment and inspect it carefully during development stages.

Lastly, for the above mentioned code, include all common sense error handling in the "perform_sql_query" function. Your perform_sql_query function is probably a wrapper over mysql_query function so check for errors generated by mysql_query (and similar) functions.

Salman A
+2  A: 

One issue to consider is that for a live site, you may not want users to see errors and warnings. Some web-host provides an error.log which logs php error. Here's a custom error handler for live sites:

function log_error($no,$msg,$file,$line)
{

    $errorMessage = "Error no $no: $msg in $file at line number $line";

    file_put_contents("errors_paypal.txt",$errorMessage."\n\n",FILE_APPEND); 
}

set_error_handler('log_error');

The great thing about this is that you can format it and dump various info you want.

Extrakun