tags:

views:

51

answers:

3

There is a way to check with a "IF" if a function fails in php?

Ex.

If (getimagesize($image) returns and error)  {
echo 'Error: function dont work';
}
else
{
// do something
}

Thanks!

A: 

Take a look at exceptions. You'll have to throw them in your function though.

Edit: By the way, if your function is not supposed to return a boolean, you can always have it return false if something goes wrong and check like:

$result = getimagesize($image);

if ($result === false)
{
    //
}
jeroen
Using exceptions for logic flow is a code smell in my book.
LiraNuna
@LiraNuna, look at the question: "If (getimagesize($image) returns and error)". The word **error** says it all; not normal logic flow so definitely a place for exceptions.
jeroen
no, its not a boolean function. Can you help me with a example code?
DomingoSL
A: 

Whatever the condition is on an "if" statement (what's inside the parenthesis) will return "true" or "false". If the condition returns "true", the first statement will be executed, otherwise the second statement will get executed.

You can put this, error_reporting(E_ALL); at the very top of your script, right after your opening php tag, to see what error you get.

Hope it helps.

Maybe something like this:

<?php
error_reporting(E_ALL);
more stuff here ...
if(your condition here){
echo "condition is true, do something";
}else{
echo "condition is not true, do something else";
}

if the function or statement you run inside the "if" or "else" fails, at least you know which one was it based on the result, and the error_reporting might tell you why it failed.

jnkrois
can you please tell me more with a code example?
DomingoSL
see above, I edited my answer.
jnkrois
+1  A: 

I 'm assuming you are interested specifically in a function like getimagesize, which does not return any error codes and makes it hard on us. But not impossible.

The documentation for getimagesize states:

If accessing the filename image is impossible, or if it isn't a valid picture, getimagesize() will generate an error of level E_WARNING. On read error, getimagesize() will generate an error of level E_NOTICE.

Therefore, you need to do two things:

  1. Get notified of the error and act upon it somehow
  2. Have the error not be displayed or otherwise affect the execution of your program at all (after all, your are going to be taking care of any errors with error-handling code yourself)

You can achieve the first one using set_error_handler() and restore_error_handler(). You can achieve the second with the error-control operator @.

So, the code must go something like this:

// Set our own error handler; we will restore the default one afterwards.
// Our new error handler need only handle E_WARNING and E_NOTICE, as per
// the documentation of getimagesize().
set_error_handler("my_error_handler", E_WARNING | E_NOTICE);

// No error has occured yet; it is the responsibility of my_error_handler
// to set $error_occurred to true if an error occurs.
$error_occurred = false; 

// Call getimagesize; use operator @ to have errors not be generated
// However, your error handler WILL STILL BE CALLED, as the documentation
// for set_error_handler() states.
$size = @getimagesize(...);

// At this point, my_error_handler will have run if an error occurred, and
// $error_occurred will be true. Before doing anything with it, restore the
// previous error handler
restore_error_handler();

if($error_occurred) {
    // whatever
}
else {
    // no error; $size holds information we can use
}


function my_error_handler($errno, $errstr, $file, $line) {
    global $error_occurred;

    // If the code is written as above, then we KNOW that an error
    // here was caused by getimagesize(). We also know what error it was:
    switch($errno) {
        case E_WARNING: // Access to image impossible, or not a valid picture
        case E_NOTICE:  // Read error
    }

    // We could also check what $file is and maybe do something based on that,
    // if this error handler is used from multiple places. However, I would not
    // recommend that. If you need more functionality, just package all of this
    // into a class and use the objects of that class to store more state.

    $error_occurred = true;
    return true; // Do not let PHP's default error handler handle this after us
}

Of course, this is not very maintainable (you have a global variable $error_occurred there, and this is not a good practice). So for a solution which not only works but is also nicely engineered, you would package all this in a class. That class would define:

  1. A method which implements the error handler (my_error_handler in the above example). To set an object method as an error handler instead of a global function, you need to call set_error_handler with a suitable first parameter; see the documentation for callback.
  2. A method which lets the class set the error handler, execute some code of your choice, save the "error happened while executing your code" flag, and restore the error handler. This method could e.g. take a callback provided by your calling code and an array of parameters and use call_user_func_array to execute it. If, during execution, the error handler set from #1 above is called, mark this in a variable in your object. Your method would return the return value of call_user_func_array to the calling code.
  3. A method or variable which the calling code can use to access the result from #2 above.

So then, if that class is called ErrorWatcher, your calling code would be something like:

$watcher = new ErrorWatcher;
$size = $watcher->watch("getimagesize",
                        array( /* params for getimagesize here */ ));

// $size holds your result, if an error did not occur;
// check for errors and we 're done!

switch($watcher->report_last_error()) {
    // error handling logic here
}

...which is nice and neat and does not mess with global variables. I hope I explained this well enough to enable you to write class ErrorWatcher yourself. :-)

Jon
Thanks, this is the best answer
DomingoSL