I’m just starting to learn PHP and am using the W3 Schools tut. In the error handling section there is this code:
<?php
function customError($errno,$errstr) {
echo '<b>Error:</b> [$errno] $errstr<br />';
}
set_error_handler('customError');
echo($test);
?>
Why is the customError() function being passed as a string? Is this a mistake in the tut?
Also, why isnt $test defined?
For the same reason that
usortorob_starttake strings for function specifiers. PHP just needs the name of the function. A function name not enclosed in quotes with either try being executed as a constant or (if followed by parenthesis) will be executed and the result passed to the function.Because of the way PHP parses documents for execution, you have to work within the means of the language (use strings instead of "pointers" to function calls).
This is a purposeful call to trigger an error. They are trying to get you to work with an undefined variable so an error occurs and the code you just wrote (with the custom handling) catches the error.