Debugging in PHP can be quite difficult if you don't know where to find the information you need. Error messages can sometimes be difficult to find and sometimes there is no direct interface with which to debug.
PHP does come with a few features that allow you to find out more information about an error that has occurred and help you get to the bottom of it.
Using the error log to find error messages in PHP
The PHP error log is one of the most useful tools for debugging, it allows you to access a log of all errors that have occurred. Every time an error occurs in PHP, information about that error such as the date and time, line on which the error occurred and the specific details about the error are printed to the log.
The error log can be found in different places depending on your operating system or application you are using as a server.
If you are using a Linux distribution such as Ubuntu, then your error log will most likely be in the /var/log/apache2 directory under a name such as PHP_error.log.
If you are using WAMP server for Windows you can actually access the error log through the WAMP interface accessed from within your system tray.
If you're really struggling to find your error log, you can check where PHP has set it to be in the php.ini file. This file is located in the root of your PHP program files. When you have found the php.ini file, locate the error_log setting to find where PHP has saved the error log.
Printing information to the error log
Using the error_log() function built in to PHP, you can print your own string to the error log. This can be very useful when you need to find out the values of variables at certain points in your script, or whether or not the script has even reached a certain point.
Sometimes you can simply use echo() to temporarily print the information to your own interface, but this is not really an option when making use of AJAX.
The error_log() function can be used anywhere in your script to print a string. For example, let's print the value of a variable to the error_log using this function:
<?php
$test = "Hello World";
error_log( $test );
?>
Now the value of $test will appear at the end of your error log:
(previous errors and warnings)
Hello world
If using echo() is a viable option in a certain situation, it might be more convenient for you to use it instead of error_log(), whatever works best for you.
Checking the response (and other data) of a request to the server directly
When using techniques such as AJAX for your website, debugging can become more of a pain than it already is. But one tool you can use is the network viewer found within the developer tools that most browsers have.
The developer tools can usually be found by pressing F12 or by locating it within the browser menu. From within the developer tools you can find the network viewer within the main navigation bar, if it does not appear there, try find a button saying "show more" and locate it there.
The network viewer shows each request that has been made to the server, including information about those requests such as:
-Requested file
-Request method (POST or GET)
-Current status of the request
-Transferred data
If you click on a specific request, you can find out even more information about it such as headers, parameters and the response of the request.
This tool allows you to see exactly what goes on with your requests, the response could even contain error messages if syntax errors or exceptions have occurred. This is mostly useful for validating the output of a PHP script.
Other debugging techniques
Sometimes you can run into bugs that are very difficult to understand, in those cases, especially with logical errors, it is useful to know a few tricks that you as a developer can use to get to the bottom of it.
If your script isn't behaving how it should or it isn't working at all, try
out suspicious lines of code and inspect the results, this could give more information about whether or not those lines are contributing to the problem.
If you simply cannot understand the issue no matter how hard you try, you can try searching the specific issue you're having on a search engine, there is a high chance that someone has already had a similar problem to yours and has solved it and shared that solution. If there is no solution to your problem on the internet, consider asking the question yourself on a forum website such as StackOverflow.
If you're having trouble understanding the piece of code you're having problems with, consider searching for more information about it, perhaps there are key aspects regarding that specific coding technique that you do not quite grasps. By learning more about it, you will be better able to solve problems relating to it.