'Conditional Statement Not Working With register_shutdown_function
I have class that builds different parts of a page (header, body, footer, etc), but when an error occurs, I want a different page to load. It works fine when an error exists, but both the page and the error page loads when there is no error. I tried to put a conditional in the header function but was told that was wrong. I'm still learning all of this, so your patience & understanding is greatly appreciated.
class template {
public function header($params) {
ob_start();
if ($this->currentTheme == '') {
$this->loadTheme('default');
}
include($path.$this->themes[$this->currentTheme]['header']);
}
public function footer($params = null) {
if ($this->currentTheme == '') {
$this->loadTheme('default');
}
include($path.$this->themes[$this->currentTheme]['footer']);
ob_end_flush();
}
}
function my_error_handler()
{
$last_error = error_get_last();
if ($last_error && $last_error['type']==E_ERROR || E_WARNING)
{
ob_end_clean();
include(__DIR__ . "/../error/500.php");
http_response_code(500);
}
}
register_shutdown_function('my_error_handler');
Solution 1:[1]
PHP's logical operators, such as || take a boolean input, and give a boolean output. They don't have any special relationship with other operators such as ==.
So this:
$last_error['type']==E_ERROR || E_WARNING
Is basically equivalent to this:
($last_error['type'] == E_ERROR)
||
(E_WARNING == true)
Which isn't what you want at all. You have to tell PHP what to compare E_WARNING against:
$last_error['type']==E_ERROR || $last_error['type']==E_WARNING
(As a side-note, you probably don't want to include Warnings here - the code will have continued after the Warning, and then run the shutdown handler at the end.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | IMSoP |
