tags:

views:

115

answers:

3
+1  Q: 

How to not die?

I want something like this:

or function() { echo mysql_error().'<br>'; } // doesn't work

Instead of:

or die(mysql_error());

I want to continue the execution of the script, not to "die". Can't I use the anonymous function that way? I just woudn't like to name it.

EDIT/TIP: For creating anonymous functions, there's a lambda-style function called create_function...

+2  A: 
function() { echo mysql_error().'<br>'; }

defines a function, it does not call it. In order to call it, you must do

$a =  function() { echo mysql_error().'<br>'; };
$a();

or use call_user_func, e.g.:

false or call_user_func(function() { echo "hi".'<br>'; });

Of course, you might as well write;

false or printf("hi<br />");

echo does not work because it's not a function; it's a language construct. (doesn't work for some other syntactic reason, since print is also a language construct and it does work).

Artefacto
Shouldn't the or automatically call it?
Alex Polo
@Alex No, the `or` evaluates the second operand if the first evaluates to false. In your case, the second operand is a definition, not a value, so it's illegal.
Artefacto
Thanks for your examples. There are valuable.
Alex Polo
+4  A: 
or print  mysql_error();

though printing it out is almost bad as dying.
In the perfect world it would be

$res = mysql_query($query) or trigger_error(mysql_error().$query);

always do it that way. You'll get not only filename and line number (which is MIGHTY helpful when you have plenty of queries on the page) but also will keep sensitive information secret on the production server, where you will turn displaying errors off and logging - on.

Col. Shrapnel
Thanks Col! trigger_error is what I wanted.
Alex Polo
A: 

If you place an @ before your function, it will suppress errors.

$example = @mysql_query(....);

Although it is generally a bad idea to use this.

phoffer
NEVER do it, son. Or you'll be neck-deep in shi... er.. trouble.
Col. Shrapnel
I told him not to do it. I just answered the question, but I made sure to say it was a bad thing to do.
phoffer
But he didn't asked for @ anyway. What question you did you try to answer? What's the use of such answers in general - "Shoot yourself in a leg. Although it is generally a bad idea to use this." - I don't understand it.
Col. Shrapnel
He said he wanted the script to continue, not to die. That's what I interpreted as the question. It doesn't matter now, you gave him the answer he wanted. I don't see why you are having such a problem with my answer. What's the use of criticizing someone unnecessarily? I don't understand it.
phoffer
If there is such an issue, might someone want to describe what the problem with using the @ sign is? Just so that people would get the idea in the first place... :)
Alex Polo
Thank you Alex, that was my thinking for this answer in the first place. Here is a warning straight from php.net: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
phoffer
@Alex that's simple. @ symbol suppress an error message. So, a programmer could not get to know what's happened forever. That's why it's considered as stupid practice. And almost noone can distinguish gagging an error message **entirely** from just cutting output to the **browser**. That's different things but every PHP user keeps using @ to not to let an error message go to **screen,** while `display_errors` setting should be used instead.
Col. Shrapnel