In PHP, a Reference Error is an anomaly that occurs whenever the web developer tries to reference a non-existent variable. This will cause a string of other related errors that will be reported by PHP. Below are three of those examples:
Notice: Undefined variable error
This is a common error that pops up whenever you use a variable that wasn’t initially defined. A good example is the code below:
Here $counter variable is used, but it is not defined first, which creates the error. To rectify this, you must first define the variable as shown below:
Trying to get property of non object error
This error will arise from trying to access a non-existent variable’s properties. An example of this is shown below:
In the above code, $users is an array and not a variable. Thus its properties cannot be derived. If the problem is not detected earlier, it escalates to a fatal error especially when trying to call a function on an invisible variable. The fatal error, in most cases, halts the entire script. The good news is that this error can be checked and rectified easily. The programmer will have to check each variable and determine if its matches the intended function. Let’s explain this in an example:
The function is trying to access the property of the nodevalue which is not a defined variable or hasn’t been checked in the collection $result. To correct this, assign variables to the object the code operates on to make it more explicit, as shown below:
A call to undefined function error
This error happens whenever a function is called that isn’t defined. The error may come from the absence of extensions, conditional function declaration or just typo mistakes. Below is an example of such an error caused by conditional function declaration:
Because $somecondition in the code above is not true, the function fn() can never be declared.
Hope the article helps some people who faced these types of common php errors. And, you are very welcome to ask more questions about this in the comments section. We will try our best to help you.