How to Debug PHP Scripts

How to debug Php code?

If on a localhost, I would suggest using firefox or chrome and installing firebug for mozilla, and chrome gets a default. Be sure that on a local host your settings are matched to the server you are uploading to as this can cause problems when going live.

Specifically most shared hosting has PHP on safe mode and output buffering off, so if you use it, use it by calling it by calling ob_start(); etc, otherwise you should have no problems, and learning to debug is part of the fun, helps you learn alot :)

As for php errors just re-edit your php.ini file, you can find al relevant information on http://php.net

Happy Coding

How do you debug PHP scripts?

Try Eclipse PDT to setup an Eclipse environment that has debugging features like you mentioned. The ability to step into the code is a much better way to debug then the old method of var_dump and print at various points to see where your flow goes wrong. When all else fails though and all I have is SSH and vim I still var_dump()/die() to find where the code goes south.

debugging long running PHP script

Profiling tool:

There is a PHP profiling tool called Blackfire which is currently in public beta. There is specific documentation on how to profile CLI applications. Once you collected the profile you can analyze the application control flow with time measurements in a nice UI:
PHP Sample Image 3

Memory consumption suspicious:

Memory seems not to be a problem, usage growing as it should, without unexpected peaks.

A growing memory usage actually sounds suspicious! If the current dataset does not depend on all previous datasets of the import, then a growing memory most probably means, that all imported datasets are kept in memory, which is bad. PHP may also frequently try to garbage collect, just to find out that there is nothing to remove from memory. Especially long running CLI tasks are affected, so be sure to read the blog post that discovered the behavior.

With an HTML form, how can I start debugging the action PHP script with proper GET/POST data? (using PhpStorm and XAMPP)

Ok, thanks to @waterloomatt I managed to find a workaround. The crux of the issue in my case is this PhpStorm bug: https://youtrack.jetbrains.com/issue/WI-54542 Therefore, this answer will probably be irrelevant once this bug is resolved.

Here's the solution on Linux :

Step 1 : Create a server on PhpStorm.

In File->Settings->PHP->Servers, click on the "+" symbol to add a new server. Fill the server infos.
How to create a server on PhpStorm
The port is 63342, which is the port PhpStorm uses for running the code. Note that I gave this server the name "localhost63342". This detail is important.

Step 2 : Launch XAMPP normally.

Launch XAMPP as you do normally. The issue is with PhpStorm, not XAMPP.

Step 3 : Before launching PhpStorm, add an environment variable

Open a command prompt, and run the following command:

export PHP_IDE_CONFIG=serverName=localhost63342

Note the server name, which is the same as the name of the server we defined in PhpStorm.

I could make that environment variable more permanent, but since it is a soon-to-be-resolved PhpStorm bug, I think that'll do.

Step 4 : In the same command prompt, launch PhpStorm

We defined an environment variable, which is valid as long as the command prompt remains open. We then can launch PhpStorm with this command prompt. In my case, with the following command:

./$HOME/phpstorm/PhpStorm-203.7148.74/bin/phpstorm.sh

Step 5 : Launch a debugging session

My Chrome browser already has the "Xdebug helper" extension: https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc?hl=en That extension is already configured to use the PHPSTORM IDE Key, as shown here:

Xdebug helper configuration

In PhpStorm, I put a breakpoint where I want in my code. I click the button to make PhpStorm listen for PHP Debug Connections. I then launch the code from the page I want using the launcher button (Chrome is Alt+F2 on my computer).

The code runs normally, and I can navigate from page to page. When, on a specific page, the code hits a PHP breakpoints, the code is suspended, and I can execute it line by line using PhpStorm's debugger. Since I filled the forms to get to this page, I get the GET/POST data.

It's not as direct as a PHP HTTP Request I suppose, but it works.

How to debug php scripts at server side? In a situation client page doesn't show response

A better approach would be to log the errors to a file, and not the 'screen'. If you make a server wide change, it's probable that other sites on the server will be affected.

  • log_errors = On
  • display_errors = Off
  • error_log = /tmp/php.log (or a better path of your choosing; file probably needs to be writable by Apache/webserver).

Failing all of the above, install xdebug and try debugging the code - you can step through line by line and inspect variables at will - perhaps this will give you a better idea.

Failing that, resort to var_dump() and friends.



Related Topics



Leave a reply



Submit