How to debug your code in PHP and most common mistakes in PHP.

Here we will see some tips and most common problems that programmers generally encounter while writing PHP code.

Common errors or mistakes and debugging tips

  • First and foremost thing is check your error.log file. For Ubuntu it is at /var/log/apache2/error.log or most Linux distribution this file is at /var/log/htttp/error.log.
  • Open a terminal and type tail -f /var/log/apache2/error.log. This command will open the file and in be in waiting mode and it will update as any changes are made it writes to the standard output. After running this command just type enter button for 2 or 3 times so that you can cleary distinguish between previous errors and new errors. Of course you can distinguish with the timestamp from the log too. Now just execute the PHP code and see the errors in the terminal. You may have a look at this image.
  • Now even after seeing the log you may have found some errors and fixed them. But still your code might not be doing what you want it to do. So now look for logical errors that you might have done. To find this errors write print/echo statement at crucial points and see if the variable is storing the value is as expected.
  • Another most common error the permission denied error when dealing with files. To solve this you can give your files that are throwing this error the www-root:www-root permission if you are on Ubuntu or apache:apache in other Linux distributions. This can be done using chown command. For example chown www-data:www-data file.txt.
  • If you are using Mysql to connect to database and a query is not executing using the variables it is receiving print that query just before it executes and then try the same query in your mysql command line interface and see if if fetches the desired result or throws any error.

These are the common tips and errors that I encounter and I had given tips from my experience. I will update this list as I do more. Thank you.