Welcome to WebmasterWorld Guest from 18.206.168.65

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Debugging PHP (a lighthearted look at our office best practices)

     
9:56 am on Sep 12, 2014 (gmt 0)

Senior Member from ES 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:July 24, 2002
posts:1129
votes: 2


Laughed so much this morning in the office at our debugging methodology. Anyone else work like this?

Finding a coding error...

You've made a change to one of your files, press refresh in the browser and the dreaded WSOD appears.
Yikes! What caused that?
1. Stare at the section you've just changed.
2. Write exit('hallo'); after each if () { declaration.
3. Refresh page in browser... cool, the WSOD has been replaced by 'hallo'. But which one?
4. Go back to code and replace exit('hallo') with exit('hallo 1'), exit('hallo 2'), exit('hallo 3'), etc
5. YES! it's line #45. Debug finished, job done.

Working with Drupal's (or other CMS) admin... (also known as click-click-guess)

We recently had a problem with views and taxonomy related content and couldn't get a field to display...
1. Type problem into google "drupal views display field related taxonomy problem".
2. Scan results and click on anything from stackoverflow or drupal.org.
3. Very briefly scan question and the accepted answer, remember a few suggested config changes.
4. Back to Drupal and fiddle around with those config options in the views admin.
Hasn't worked? Back to the search results and repeat... Remember there's no point in reading the full replies as nobody else's problem will be exactly like yours - all you need is a pointer in the right direction!

I can't tell you how many times my colleague and I have worked like this and we have nearly 20 years experience in PHP between us - lol!

We do have other debugging tools at our disposal, such as tailing the error log, or turning on errors by appending a URL parameter, but most of the time we just hack away ;)
5:46 pm on Sept 12, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15496
votes: 747


Goodness. That is positively IDENTICAL to the way I debug javascript. Mutatis mutandis. Except I say
alert("here1");
alert("here2");

et cetera* ad lib, with a preliminary
alert("this file OK");

when I'm in doubt about the file as a whole.

In php it's
echo $some-variable-here

to assure myself that something, at least, is happening. Needless to say, the problem is never in the transmission of the variable. OK, now give me some hint why you saw fit not to include the file...


* I've actually got a <p id = "debugger"> that collects and displays all the info I ask for-- but it can only be used after I've passed some preliminary essentials. Like, well, the function that sends text to the debugger in the first place.
9:43 am on Sept 15, 2014 (gmt 0)

Senior Member from ES 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:July 24, 2002
posts:1129
votes: 2


:) that's the beauty of it - works with all languages!

my task for this week is get a proper debugger setup (xdebug) or similar
1:42 pm on Sept 16, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


debug javascript. ...
alert("here1");


If JS then use the console (all modern browsers) instead:
console.log('here1');
11:11 pm on Sept 16, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15496
votes: 747


use the console (all modern browsers) instead

Now to see if I will still remember this the next time I'm using js for something other than a game (where it's useful to have an error message jump up and hit me in the face the moment I try a particular input :)).

Luckily MAMP does php error logging by default, so I can always find something. Even if it's only reporting a failure to find such-and-such file ... when the whole purpose of the function was to check whether the file exists*, so it really shouldn't get so worked up.


* "If you find this particular cookie, take action A, otherwise take action B."
1:39 pm on Oct 24, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:945
votes: 0


debug javascript. ...
alert("here1");



If JS then use the console (all modern browsers) instead:
console.log('here1');

To build on this - javascript execution can often be affected by dom/external file load, multithreading brought on by timeouts and different events firing etc.

alert() stops all javascript in the current thread, so can be misleading (as if the problem is other threads that need to finish before current thread not doing so, the delay between alert() and user interaction can allow those threads to catch up, causing the problem thread to not exhibit the problem). console.log does not have this problem.

Also, console.log prints the file/line number in the console in Firefox (presumably it does so on other browsers too, I just don't know which ones :))
11:56 am on Dec 26, 2014 (gmt 0)

Senior Member from ES 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:July 24, 2002
posts:1129
votes: 2


just to add to this... i have finally changed IDE from Dreamweaver (don't laugh) to Sublime Text and whilst at it have XDebug running on my VM which I control through Sublime Text. now that's proper debugging! ;)

p.s. i love dreamweaver because it has the best out of the box syntax highlighting and also php/js error checking without setting up local servers. i have since downloaded a dreamweaver theme for Sublime (!) and set up a local windows wamp server just to get php/js error checking working for Sublime.