Avoiding Simple Errors

Posted by on in Blogs

On this post, we will have a look at two kinds of PHP errors, and learn how we can save time using RadPHP to avoid them.


Syntax Errors


Syntax errors are the most fundamental errors, those produced by a PHP code that cannot be understood by the PHP interpreter, which reports them as “Parse error”.  Common examples of syntax errors are a missing semi-colon at the end of a line of code, not escaping quotes inside a string, forgetting to write an initial or final parenthesis…


To avoid these errors you have Error Insight, a syntax checking tool that reviews the code as you write it, warning you about these errors by underlining them in red on the Code Editor, and listing them on the Structure widget.


Calls to Undefined Elements


Also common are those errors produced by a call to a PHP element that does not exist, either because we misspelled it or because we forgot to include the script defining the element. PHP will exit with an error line, like Call to undefined function or Class ‘[class]’ not found.


To avoid these errors we have the Code Insight, a powerful feature of the Code Editor that will help you write code easier, faster and avoiding the use of unexisting PHP elements.


To take advantage of this feature, just write the beginning of the code element (variable, function, property, method…) and hit the Code Insight shortcut, Ctrl + Space by default. You will then get a list of possible elements, and you can navigate that list, reading for each element its documentation to help you decide which one to use.


For example, if you write str somewhere in your code, and hit the shortcut, the list of possibilities in the current context will be displayed:



You can continue writting after the Code Insight comes in, and the list of possibilities will be filtered as you do so. Following the example above, if we were to type the l character then, the list would get updated displaying only the results still matching our written code:



Sometimes, you do not even have to trigger the Code Insight, it gets triggered automatically. This happens, for example, when you are about to call a method on an object:



Last but not least, Code Insight lets you use Code Templates. Code templates are common code structures that you can print following the same logic you follow with the autocompletion feature, only that you have to hit Ctrl+J instead.



There are a lot of default code templates you can use, but you can also customize them so they match your style, or add new ones to fit your needs.


The Tricky Ones


These errors were easy to avoid, since RadPHP automatically detects syntax errors and warns you about them, and it gives you the tools to avoid calls to undefined elements while working faster on your code.


The tricky ones are the logic errors, those where the PHP interpreter does not complain, but the application does not work as expected. For those, we will use the Debugger.




Comments

Check out more tips and tricks in this development video: