How can I catch ALL errors in PHP? - php

I've found a lot of attempts at all-inclusive error handling implementations, and I figured I might write up a wiki-style to hopefully provide a complete solution that I came up with.
The question is:
"How might one catch, handle, or intercept ALL error types in PHP?"
Now - this might be deemed a 'rewrite' by some - but I don't know that there's been a comprehensive solution proposed.

There are three kinds of error handlers you need:
set_exception_handler, to catch any otherwise uncaught exceptions.
set_error_handler to catch "standard" PHP errors. I like to first check it against error_reporting to see if it's an error that should be handled or ignored (I generally ignore Notices - probably bad but that's my choice), and throw an ErrorException, letting the exception handler catch it for outputting.
register_shutdown_function combined with error_get_last. Check the value of ['type'] to see if it is E_ERROR, E_PARSE or basically any fatal error types you want to catch. These normally bypass set_error_handler, so catching them here lets you grab them. Again, I tend to just throw an ErrorException, so that all errors end up being handled by the same exception handler.
As for how you implement these, it depends entirely on how your code is set up. I usually do an ob_end_clean() to purge any output, and present a nice error page telling them that the error has been reported.

There are a number of levels of PHP errors, some of these require setting separate error handlers, and in order to catch every error PHP might throw up - you must write something that encompasses all of these 'types' of errors -- startup, 'runtime', and exceptions.
My solution to catch every ( as far as I can tell ) error that comes down the pipe:
A couple of global variables
An initialization method
4 'non-OOP' error handler methods
An ErrorHandler class with an 'AppendError' method - which is where one might make modifications to how exactly errors are output or not ( in this case, errors are just dumped to the screen in some minimal HTML from this method )
...
// Moved this line to the bottom of the 'file' for usability -
// I keep each of the above mentioned 'pieces' in separate files.
//$ErrorHandler = new ErrorHandler();
$ErrorCallback = "HandleRuntimeError";
$ExceptionCallback = "HandleException";
$FatalCallback = "HandleFatalError";
$EnableReporting = true;
$ErrorLevel = E_ALL;
function InitializeErrors()
{
if($GLOBALS["EnableReporting"])
{
error_reporting($GLOBALS["ErrorLevel"]);
if( isset($GLOBALS["ErrorCallback"]) && strlen($GLOBALS["ErrorCallback"]) > 0 )
{
set_error_handler($GLOBALS["ErrorCallback"]);
// Prevent the PHP engine from displaying runtime errors on its own
ini_set('display_errors',false);
}
else
ini_set('display_errors',true);
if( isset($GLOBALS["FatalCallback"]) && strlen($GLOBALS["FatalCallback"]) > 0 )
{
register_shutdown_function($GLOBALS["FatalCallback"]);
// Prevent the PHP engine from displaying fatal errors on its own
ini_set('display_startup_errors',false);
}
else
ini_set('display_startup_errors',true);
if( isset($GLOBALS['ExceptionCallback']) && strlen($GLOBALS['ExceptionCallback']) > 0 )
set_exception_handler($GLOBALS["ExceptionCallback"]);
}
else
{
ini_set('display_errors',0);
ini_set('display_startup_errors',0);
error_reporting(0);
}
}
function HandleRuntimeError($ErrorLevel,$ErrorMessage,$ErrorFile=null,$ErrorLine=null,$ErrorContext=null)
{
if( isset($GLOBALS['ErrorHandler']))
{
// Pass errors up to the global ErrorHandler to be later inserted into
// final output at the appropriate time.
$GLOBALS['ErrorHandler']->AppendError($ErrorLevel,"Runtime Error: " . $ErrorMessage,$ErrorFile,$ErrorLine,$ErrorContext);
return true;
}
else
{
PrintError($ErrorLevel,$ErrorMessage,$ErrorFile,$ErrorLine,$ErrorContext);
return true;
}
}
function HandleException($Exception)
{
if( isset($GLOBALS['ErrorCallback']))
{
// Parse and pass exceptions up to the standard error callback.
$GLOBALS['ErrorCallback']($Exception->getCode(), "Exception: " . $Exception->getMessage(), $Exception->getFile(), $Exception->getLine(), $Exception->getTrace());
return true;
}
else
{
PrintError($Exception->getCode(), "Exception: " . $Exception->getMessage(), $Exception->getFile(), $Exception->getLine(), $Exception->getTrace());
return true;
}
}
function HandleFatalError()
{
$Error = error_get_last();
// Unset Error Type and Message implies a proper shutdown.
if( !isset($Error['type']) && !isset($Error['message']))
exit();
else if( isset($GLOBALS['ErrorCallback']))
{
// Pass fatal errors up to the standard error callback.
$GLOBALS["ErrorCallback"]($Error['type'], "Fatal Error: " . $Error['message'],$Error['file'],$Error['line']);
return null;
}
else
{
PrintError($Error['type'], "Fatal Error: " . $Error['message'],$Error['file'],$Error['line']);
return null;
}
}
// In the event that our 'ErrorHandler' class is in fact the generator of the error,
// we need a plain-Jane method that will still deliver the message.
function PrintError($ErrorLevel,$ErrorMessage,$ErrorFile=null,$ErrorLine=null,$ErrorContext=null)
{
if( class_exists("ErrorHandler"))
$ErrorTypeString = ErrorHandler::ErrorTypeString($ErrorLevel);
else
$ErrorTypeString = "$ErrorLevel";
if( isset($ErrorContext) && !is_array($ErrorContext) && strlen($ErrorContext) > 0 )
$ErrorContext = str_replace("#", "<br/>\r\n#", $ErrorContext);
$ReturnValue = "";
$ReturnValue .= "<div class=\"$ErrorTypeString\" style=\"margin: 10px;\">\r\n";
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Error Level:</span> <span class=\"ErrorValue\">$ErrorTypeString</span></p>\r\n";
if( isset($ErrorFile) && strlen($ErrorFile) > 0 )
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">File:</span> <span class=\"ErrorValue\">'$ErrorFile'</span></p>\r\n";
if( isset($ErrorLine) && strlen($ErrorLine) > 0 )
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Line:</span> <span class=\"ErrorValue\">$ErrorLine</span></p>\r\n";
if( isset($ErrorContext) && is_array($ErrorContext))
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Context:</span><span class=\"ErrorValue\">" . var_export($ErrorContext,true) . "</span></p>\r\n";
else if( isset($ErrorContext) && strlen($ErrorContext) > 0 )
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Context:</span><span class=\"ErrorValue\">$ErrorContext</span></p>\r\n";
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Message:</span> <span class=\"ErrorValue\">" . str_replace("\r\n","<br/>\r\n",$ErrorMessage) . "</span></p>\r\n";
$ReturnValue .= "</div>\r\n";
echo($ReturnValue);
}
class ErrorHandler
{
public function AppendError($ErrorLevel,$ErrorMessage,$ErrorFile=null,$ErrorLine=null,$ErrorContext=null)
{
// Perhaps evaluate the error level and respond accordingly
//
// In the event that this actually gets used, something that might
// determine if you're in a production environment or not, or that
// determines if you're an admin or not - or something - could belong here.
// Redirects or response messages accordingly.
$ErrorTypeString = ErrorHandler::ErrorTypeString($ErrorLevel);
if( isset($ErrorContext) && !is_array($ErrorContext) && strlen($ErrorContext) > 0 )
$ErrorContext = str_replace("#", "<br/>\r\n#", $ErrorContext);
$ReturnValue = "";
$ReturnValue .= "<div class=\"$ErrorTypeString\" style=\"margin: 10px;\">\r\n";
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Error Level:</span> <span class=\"ErrorValue\">$ErrorTypeString</span></p>\r\n";
if( isset($ErrorFile) && strlen($ErrorFile) > 0 )
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">File:</span> <span class=\"ErrorValue\">'$ErrorFile'</span></p>\r\n";
if( isset($ErrorLine) && strlen($ErrorLine) > 0 )
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Line:</span> <span class=\"ErrorValue\">$ErrorLine</span></p>\r\n";
if( isset($ErrorContext) && is_array($ErrorContext))
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Context:</span><span class=\"ErrorValue\">" . var_export($ErrorContext,true) . "</span></p>\r\n";
else if( isset($ErrorContext) && strlen($ErrorContext) > 0 )
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Context:</span><span class=\"ErrorValue\">$ErrorContext</span></p>\r\n";
$ReturnValue .= "<p class=\"ErrorData\"><span class=\"ErrorKey\">Message:</span> <span class=\"ErrorValue\">" . str_replace("\r\n","<br/>\r\n",$ErrorMessage) . "</span></p>\r\n";
$ReturnValue .= "</div>\r\n";
echo($ReturnValue);
}
public static function ErrorTypeString($ErrorType)
{
$ReturnValue = "";
switch( $ErrorType )
{
default:
$ReturnValue = "E_UNSPECIFIED_ERROR";
break;
case E_ERROR: // 1 //
$ReturnValue = 'E_ERROR';
break;
case E_WARNING: // 2 //
$ReturnValue = 'E_WARNING';
break;
case E_PARSE: // 4 //
$ReturnValue = 'E_PARSE';
break;
case E_NOTICE: // 8 //
$ReturnValue = 'E_NOTICE';
break;
case E_CORE_ERROR: // 16 //
$ReturnValue = 'E_CORE_ERROR';
break;
case E_CORE_WARNING: // 32 //
$ReturnValue = 'E_CORE_WARNING';
break;
case E_COMPILE_ERROR: // 64 //
$ReturnValue = 'E_COMPILE_ERROR';
break;
case E_CORE_WARNING: // 128 //
$ReturnValue = 'E_COMPILE_WARNING';
break;
case E_USER_ERROR: // 256 //
$ReturnValue = 'E_USER_ERROR';
break;
case E_USER_WARNING: // 512 //
$ReturnValue = 'E_USER_WARNING';
break;
case E_USER_NOTICE: // 1024 //
$ReturnValue = 'E_USER_NOTICE';
break;
case E_STRICT: // 2048 //
$ReturnValue = 'E_STRICT';
break;
case E_RECOVERABLE_ERROR: // 4096 //
$ReturnValue = 'E_RECOVERABLE_ERROR';
break;
case E_DEPRECATED: // 8192 //
$ReturnValue = 'E_DEPRECATED';
break;
case E_USER_DEPRECATED: // 16384 //
$ReturnValue = 'E_USER_DEPRECATED';
break;
}
return $ReturnValue;
}
}
$ErrorHandler = new ErrorHandler();
Now - code out of the way...
To implement this, it's as simple as including this file, and executing the 'InitializeErrors' method. Beyond this, it's up to you what you want to Do with the errors; this is simply a wrapper for every error that PHP might generate - and to make changes to how any given error is handled, it's basically as simple as evaluating it in the 'AppendError' method and responding accordingly.
-- I should note - I implemented return values for reasons I cannot explain, and I should review my own work on that front - but I'm not terribly sure it has any bearing on the result.

Into file [Config.php] in [phpMyAdmin-4.6.5.2-all-languages] you can find:
/**
* Error handler to catch fatal errors when loading configuration
* file
*
*
* PMA_Config_fatalErrorHandler
* #return void
*/
public static function fatalErrorHandler()
{
if (!isset($GLOBALS['pma_config_loading'])
|| !$GLOBALS['pma_config_loading']
) {
return;
}
$error = error_get_last();
if ($error === null) {
return;
}
PMA_fatalError(
sprintf(
'Failed to load phpMyAdmin configuration (%s:%s): %s',
Error::relPath($error['file']),
$error['line'],
$error['message']
)
);
}
and just in end of file:
register_shutdown_function(array('PMA\libraries\Config', 'fatalErrorHandler'));

Related

Uncaught Error: Attempt to assign property "filename" on null

The below error message is triggered in PHP 8:
Uncaught Error: Attempt to assign property "filename" on null in
This points to this line of code:
$pluginInfo->filename = $this->pluginFile;
The whole section of code is:
$pluginInfo = null;
if ( !is_wp_error($result) && isset($result['response']['code']) && ($result['response']['code'] == 200) && !empty($result['body']) ){
$pluginInfo = PluginInfo_2_2::fromJson($result['body'], $this->debugMode);
$pluginInfo->filename = $this->pluginFile;
$pluginInfo->slug = $this->slug;
} else if ( $this->debugMode ) {
$message = sprintf("The URL %s does not point to a valid plugin metadata file. ", $url);
if ( is_wp_error($result) ) {
$message .= "WP HTTP error: " . $result->get_error_message();
} else if ( isset($result['response']['code']) ) {
$message .= "HTTP response code is " . $result['response']['code'] . " (expected: 200)";
} else {
$message .= "wp_remote_get() returned an unexpected result.";
}
trigger_error($message, E_USER_WARNING);
}
$pluginInfo = apply_filters('puc_request_info_result-'.$this->slug, $pluginInfo, $result);
return $pluginInfo;
}
Still at early stages of learning PHP, and struggling to rewrite this code for PHP 8 compatibility.
Any help would be appreciated.
Only current fix is to go back to PHP 7.4.

Is it possible to debug this PHP

I'm really grasping at straws here. I don't code and my PHP email extension works great EXCEPT for these errors when editing the templates. My vendor is in who-knows what timezone and has been very unresponsive. Besides the obvious - change extensions, change vendors etc., can any of you PHP folk look at this code block and tell me what's wrong. I don't know if taken out of context it will be meaningful. The Errors are PHP Notice: Trying to get property of non-object beginning at line 1344. Here is a block of code from 1344 to 1370. It evidently has to do with switching to plain text when HTML isn't an option. AGAIN, grasping at straws here but stackoverflow is pretty solid when it comes to knowledgeable folk.
I'm re-pasting the code to include a more complete block. I have also added comments in the lines to indicate where the four warnings appear. They look like this:
//NEXT LINE IS ERROR ONE - LINE 1344 Notice: Trying to get property of non-object
Thank you
<?php
public static function isHTML($str) {
$html = array('A','ABBR','ACRONYM','ADDRESS','APPLET','AREA','B','BASE','BASEFONT','BDO','BIG','BLOCKQUOTE',
'BODY','BR','BUTTON','CAPTION','CENTER','CITE','CODE','COL','COLGROUP','DD','DEL','DFN','DIR','DIV','DL',
'DT','EM','FIELDSET','FONT','FORM','FRAME','FRAMESET','H1','H2','H3','H4','H5','H6','HEAD','HR','HTML',
'I','IFRAME','IMG','INPUT','INS','ISINDEX','KBD','LABEL','LEGEND','LI','LINK','MAP','MENU','META',
'NOFRAMES','NOSCRIPT','OBJECT','OL','OPTGROUP','OPTION','P','PARAM','PRE','Q','S','SAMP','SCRIPT',
'SELECT','SMALL','SPAN','STRIKE','STRONG','STYLE','SUB','SUP','TABLE','TBODY','TD','TEXTAREA','TFOOT',
'TH','THEAD','TITLE','TR','TT','U','UL','VAR');
return preg_match("~(<\/?)\b(".implode('|', $html).")\b([^>]*>)~i", $str, $c);
}
private function _html_to_plain_text($node) {
if ($node instanceof DOMText) {
return preg_replace("/\\s+/im", " ", $node->wholeText);
}
if ($node instanceof DOMDocumentType) {
// ignore
return "";
}
// Next
//NEXT LINE IS ERROR ONE - LINE 1344 Notice: Trying to get property of non-object
$nextNode = $node->nextSibling;
while ($nextNode != null) {
if ($nextNode instanceof DOMElement) {
break;
}
$nextNode = $nextNode->nextSibling;
}
$nextName = null;
if ($nextNode instanceof DOMElement && $nextNode != null) {
$nextName = strtolower($nextNode->nodeName);
}
// Previous
//NEXT LINE IS ERROR TWO - LINE 1357 Notice: Trying to get property of non-object
$nextNode = $node->previousSibling;
while ($nextNode != null) {
if ($nextNode instanceof DOMElement) {
break;
}
$nextNode = $nextNode->previousSibling;
}
$prevName = null;
if ($nextNode instanceof DOMElement && $nextNode != null) {
$prevName = strtolower($nextNode->nodeName);
}
//NEXT LINE IS ERROR THREE - LINE 1369 Notice: Trying to get property of non-object
$name = strtolower($node->nodeName);
// start whitespace
switch ($name) {
case "hr":
return "------\n";
case "style":
case "head":
case "title":
case "meta":
case "script":
// ignore these tags
return "";
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
// add two newlines
$output = "\n";
break;
case "p":
case "div":
// add one line
$output = "\n";
break;
default:
// print out contents of unknown tags
$output = "";
break;
}
// debug $output .= "[$name,$nextName]";
//NEXT LINE IS ERROR FOUR - LINE 1408 Notice: Trying to get property of non-object
if($node->childNodes){
for ($i = 0; $i < $node->childNodes->length; $i++) {
$n = $node->childNodes->item($i);
$text = $this->_html_to_plain_text($n);
$output .= $text;
}
}
// end whitespace
switch ($name) {
case "style":
case "head":
case "title":
case "meta":
case "script":
// ignore these tags
return "";
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
$output .= "\n";
break;
case "p":
case "br":
// add one line
if ($nextName != "div")
$output .= "\n";
break;
case "div":
// add one line only if the next child isn't a div
if (($nextName != "div" && $nextName != null) || ($node->hasAttribute('class') && strstr($node->getAttribute('class'), 'emailtemplateSpacing')))
$output .= "\n";
break;
case "a":
// links are returned in [text](link) format
$href = $node->getAttribute("href");
if ($href == null) {
// it doesn't link anywhere
if ($node->getAttribute("name") != null) {
$output = "$output";
}
} else {
if ($href == $output || ($node->hasAttribute('class') && strstr($node->getAttribute('class'), 'emailtemplateNoDisplay'))) {
// link to the same address: just use link
$output;
} else {
// No display
$output = $href . "\n" . $output;
}
}
// does the next node require additional whitespace?
switch ($nextName) {
case "h1": case "h2": case "h3": case "h4": case "h5": case "h6":
$output .= "\n";
break;
}
default:
// do nothing
}
return $output;
}
}
?>
Sounds like the $node variable coming into your function from private function _html_to_plain_text($node) isn't the correct variable type. Might be an int, string, array, or null, instead of whatever object/class it's supposed to be. You probably need to look at whatever code is calling that function, rather than the function itself.
Debugging in PHP is a skill in itself.
If coding by hand, consider inserting var_export($node); statements before and after areas where you suspect there is a problem so you can examine it.
If you have access to a debugger (for example, Eclipse + XAMPP + XDebug), use that and go line by line, examining the contents of the $node variable by hovering over it or looking at your variable list.

PHP - Return array from function

I am submitting a form, using the following function (prize.php):
loadmodule('validate'); //This just loads the validate.php function.
$validate = new Validate;
if($_POST)
{
$validateForm = $validate->validateForm();
switch($validateForm)
{
case 1:
$error = 'You\'re not logged in..';
$stop = true;
break;
//If no error = success.
if($validateForm['code'] == "100"){
$won = $val['prize'];
$type = $val['type'];
$success = 'You have won! The prize was '.$won.' '.$type.'';
die($success);
}
}
die($error);
}
This is the function to validate the form (validate.php):
function validate()
{
global $userdata;
if(!is_array($userdata))
return 1; // User not logged in - return error code one.
//If no error, lets show a success message.
$prize = "100";
$text = "dollars";
return array("code"=>"100","prize"=>"$prize","type"=>"$text");
}//If won
}
The above code returns:
Notice: Undefined variable: error in /home/.../public_html/pages/prize.php on line 27
Although, it shouldn't throw an error there, since the die($success) should be triggered by the code 100.
What am I doing wrong?
$error = '';
$stop = false;
switch($validateForm){
case 1:
$error = 'You\'re not logged in..';
$stop = true;
break;
}
//If no error = success.
if($validateForm['code'] == "100"){
$won = $val['prize'];
$type = $val['type'];
$success = 'You have won! The prize was '.$won.' '.$type.'';
die($success);
}
first guess is that the if($validateForm['code'] == "100"){ is ment to be outside the switch.
$validateForm = $validate->validateForm();
returns an array.. later on your're doing a if ($validateForm==1) in the switch.. when $validateForm is an array.
you might have better luck with a simple is_array() if statement than the whole switch

Defined error message in the PHP

How do I "make" error messages in best practices? Right now I just have $error = ''; and then just a lot of if(){}s that do .= 'Message<br>', and then if (!empty($error)) { echo $error; }, which I assume is a terrible thing.
I guess you want to show error to user. I usually use an array.
$success = true;
$messages = array();
if($something_wrong == true){
$success = false;
$messages[] = 'something wrong';
}
if($morething_wrong == true){
$success = false;
$messages[] = 'more thing wrong';
}
if(!$success){
foreach($messages as $message){
echo $message.'<br />';
}
}
Basically if things goes wrong, I'll always assign the message as the new last component of $messages. In the end of execution, I show all of the messages.
Hope this helps
one way to do it:
function err($err){
switch($err){
case 0: $message.="brain fried"; break; // comment if you want
case 1: $message.="head ache"; break;
case 2: $message.="segment fault big time"; break;
}
return $message;
}
And
if (something is wrong){$message=err(3);}
Just a way to group things all together.

How to clean and simplify this code?

After thinking about This Question and giving an answer to it I wanted to do more about that to train myself.
So I wrote a function which will calc the length of an given function. Th given php-file has to start at the beginning of the needed function.
Example: If the function is in a big phpfile with lots of functions, like
/* lots of functions */
function f_interesting($arg) {
/* function */
}
/* lots of other functions */
then $part3 of my function will require to begin like that (after the starting-{ of the interesting function):
/* function */
}
/* lots of other functions */
Now that's not the problem, but I would like to know if there are an cleaner or simplier ways to do this. Here's my function: (I already cleaned a lot of testing-echo-commands)
(The idea behind it is explained here)
function f_analysis ($part3) {
if(isset($part3)) {
$char_array = str_split($part3); //get array of chars
$end_key = false; //length of function
$depth = 0; //How much of unclosed '{'
$in_sstr = false; //is next char inside in ''-String?
$in_dstr = false; //is nect char inside an ""-String?
$in_sl_comment = false; //inside an //-comment?
$in_ml_comment = false; //inside an /* */-comment?
$may_comment = false; //was the last char an '/' which can start a comment?
$may_ml_comment_end = false; //was the last char an '*' which may end a /**/-comment?
foreach($char_array as $key=>$char) {
if($in_sstr) {
if ($char == "'") {
$in_sstr = false;
}
}
else if($in_dstr) {
if($char == '"') {
$in_dstr = false;
}
}
else if($in_sl_comment) {
if($char == "\n") {
$in_sl_comment = false;
}
}
else if($in_ml_comment) {
if($may_ml_comment_end) {
$may_ml_comment_end = false;
if($char == '/') {
$in_ml_comment = false;
}
}
if($char == '*') {
$may_ml_comment_end = true;
}
}
else if ($may_comment) {
if($char == '/') {
$in_sl_comment = true;
}
else if($char == '*') {
$in_ml_comment = true;
}
$may_comment = false;
}
else {
switch ($char) {
case '{':
$depth++;
break;
case '}':
$depth--;
break;
case '/':
$may_comment = true;
break;
case '"':
$in_dstr = true;
break;
case "'":
$in_sstr = true;
break;
}
}
if($depth < 0) {
$last_key = $key;
break;
}
}
} else echo '<br>$part3 of f_analysis not set!';
return ($last_key===false) ? false : $last_key+1; //will be false or the length of the function
}
Tokenizer (Example) - Learn it, love it.
You could probably reduce the number of state variables a little, but truthfully... yes, it will be messy code. I would probably get rid of $may_ml_comment_end and peek ahead for the next character when I encounter an asterisk, for example. You will need to rewrite your foreach loop to a regular for loop be able to do that without creating a bigger mess though.
PS: I don't see you handling the escape character yet. Without the above approach, that would introduce another boolean variable.
Another problem with your current code is that characters immediately following a / don't get interpreted as they should. However unlikely
echo 5/'2'; // NB: no space in between
is valid in PHP and would break your parser.

Categories