Of course, you can put echo or print statements somewhere in the debugged php file to print some messages or value of variables. More convenient way is using an IDE with single-step debugging functionality. Eclipse is such an IDE, and free.
Eclipse has various versions for different programming laugage. For php, download
This is the Eclipse php IDE which is free and does not need installation. Just unpack the downloaded file to a folder and run eclipse.exe in that folder. But you may need to install JVM to run Eclipse. There are several software related to JVM on Oracle’s website. Eclipse only needs JRE(Java SE Runtime Environment 8 Downloads), so you do not need to download JDK(which is bigger). After downloading jre package, unpack it to the Eclipse folder, i.e., if you unpack Eclipse to C:\Eclipse(eclipse.exe in this folder), unpack jre package to C:\Eclipse\jre . Now you should be able to launch eclipse.exe.
Although Eclipse has good fame on the Internet, I find it is not very easy to set it up. The configuration is confused. First there are several kinds of directories to confuse you when you create a project: the workspace location, the source code directory, the web directory. Second, I cannot move the current instruction pointer forward when single-step debugging. It seems the debug step over not working although I know the debug is going on and not disabled. Third, the output is integrated into a small embedded browser in the IDE, which does not look the same as you usually do in your browser.
So I recommend another php IDE: Netbean IDE. You need to install both JDK and netbean package for php(https://netbeans.org/downloads). Both software have an installer so you won’t make mistake about which directories they are installed into. After installation, it is ready to debug your php by creating a php project. The php project is created based on new php file or existing php files. The only thing you should notice is putting the project directory in your web directory (usually under the htdocs/ folder) if you want to debug php code with xampp or wamp . And the debug GUI is concise and easy to use. The best part is web page is displayed in your favorite browser so the web content won’t mess with the IDE content and you can debug php scripts with chrome, firefox, or whatever browser. With NetBeans IDE, you can debug php code in the following ways:
- debug php code on a remote server. NetBeans will download php files from the remote server to show in the IDE and upload your changes to the server. This way it gives you the impression that you are debugging the php scripts online.
- debug php code on your local server. With the help of xampp or wamp, you can put your code in a web directory on your local machine, and configure a NetBeans project to use url like localhost/yourwebsite.
- debug php code with a command line php interpreter. This way you do not even need to start an apache server.
Make sure the xdebug options are correctly set in php.ini. Netbeans communicate with xdebug to get the debug information(you may have noticed the hint message: waiting for connection(netbeans xdebug)…”). If xdebug is not configured properly, Netbeans cannot single-step your php script and won’t give you a hint about this problem. So when you begin to debug your website in Netbeans and find your website is opened immediately without stop, please check the xdebug settings. Remove the “;” before the option lines and set xdebug.remote_enable = 1.
IDE is the best tool to do step-debug. If you do not want to use an IDE, you can set proper configuration variables in php.ini to help debug problems. The most important variables are: display_errors and error_reporting. After setting display_errors=On and error_reporting=E_ALL, you can see the error messages on the browser’s window when you visit a php website and there is some error on the page you are visiting. In production environment, display_errors is usually turned off, so you can not see the error messages on browser’s window. But that does not mean you can not find the error anywhere. Error messages are always recorded in Apache’s error log file.