I am coding within php file that is receiving data from AJAX. I am doing something with that data withing pjp file and would like to be able to see errors while I am writing code. Since this.php file is not rendering in the browser and therefore I cant see errors that I will be receiving how can I easily see errors,
I know I can do to apache log and maybe try to pull last 10 lines …something like this…
That is a really inefficient way to develop. You should look into a local environment and smart debugging. The only time you should be doing that is when the problem can’t be recreated locally and you absolutely must implement debugging measures on production.
With smart debugging you can call a php script using AJAX and stop the script in real time at the server and go line by line and execute it until you find the error. Using smart debugging doesn’t have anything to do with whether the script is meant to executed at the command line or output to the browser for display.
Then outputting it to the screen should work. If you are trying to figure out why there’s errors, error.log is where you go. Otherwise, this is not useful information if you want to find out what the value is. If something doesn’t work right in a logic, I normally use die() and I go one step at a time. If the die() is gone, that’s when you know it failed or triggers the other logic which would be something you want to debug more and find out why it is doing what it’s doing.
Any output during the script execution will be included in the response body (actually, is the response body), and you can always see the full response in the network panel of the browser dev tools (which also provide some nice features such as appropriately formatting the data, or even rendering HTML). Of course, doing so will likely lead to invalid data your JS won’t understand… e.g. if you expect a JSON response, echoing or var_dump()ing random values will give you something like
Debug message: the eggs are overcooking!
{
"data": "This should be the actual response"
}
and JSON.parse() will throw an error. So in most cases you’ll want to log your debug info to a file, which you can monitor with tail -f like @Andres_Vaquero suggested… other than that, you might have a look at xdebug for some more sophisticated debugging with breakpoints and all.
I confess I use echo, print_r and var_dump , and even alert() instead of console.log() at times - in pre-alpha code for quick and dirty debugging. It works OK for experimenting with short and simple code. True, xdebug has a bit of a learning curve getting it installed, configured and learning how to use. But if you want to get into seriously writing code it (or some other debugging tool) is well worth using and you will not regret it.