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 calle开发者_高级运维d create_function...
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.
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).
If you place an @ before your function, it will suppress errors.
$example = @mysql_query(....);
Although it is generally a bad idea to use this.
精彩评论