Using SyntaxHighlighter to Format Code in WordPress

Based on a question in the StackOverflow beta site, I did some quick research into what are the best ways to perform syntax highlighting on code that is posted on blogs. Among the methods that were suggested (by myself or others):

  1. Hack together your own display logic to format it as you see fit
  2. Use the SyntaxHighlighter JavaScript library
  3. Use Windows Live Writer with the Insert Code plugin (I discuss that here)
  4. For WordPress, use the WP-Syntax plugin

Coincidentally, I had heard Scott Hanselman talking about how he does code formatting just a couple of days ago, in Hanselminutes #125, where he described how he posted code on his blog by putting it inside <pre></pre> tags, adding specific name and class attributes, and letting some JavaScript library do the formatting work. So I went to his blog, opened up a post with some code, and found my way to the SyntaxHighlighter JavaScript library. This is a very nifty library that handles formatting very nicely for a number of popular programming and scripting languages, and seemed to have a very easy implementation. So I decided to implement it for formatting code on my site.
(more…)

Deciphering PHP Error Messages and Blank Screens

My normal home is Visual Studio, ASP.net (1.1/2.0) and C#, with some CSS, HTML and JavaScript thrown in. Over the past week however, I have been working on an add-on to a client site written in PHP (using mySql as a DB backend).

I am coding in Dreamweaver and am really missing Visual Studio, intellisense, robust debugging, design-time compile, being told right away when I make syntax mistakes and intelligent error messages. Since I have to upload to the test server before trying anything out, I have had to spend too much of my time trying to figure out why errors are occuring, backtracking, commenting out lines, upload and try again.

To save myself some googling in the future, and perhaps to help others in the same quandry, below are some of the most common error messages (or lack thereof) that I experienced during development, along with what they actually mean (since the words themselves rarely tell you what you need to know). If anyone has more to add, put them in the comments and I will add them below.

Parse error: parse error, unexpected $ – You forgot to close all of your brackets. (Or maybe you wrote an open-bracket sign – { – instead of a close-bracket sign)

Completely Blank Screen – This one is the worst. It happens when you mess up php syntax. Note: the syntax error may be on the referenced page, or it may be on some page that you included, or that was included by an include (etc). The best part about it is you have no way of knowing where the error originated since the blank screen conveniently forgets to provide you with any error message. Here are the different things that caused this one to appear for me:

  • Forgot a semi-colon at the end of a line. (This one is the most frustrating, because it is sometimes very hard to find, and because if I was using VS, I would find out about it when I was compiling my site, and not when I was trying to test)
  • Include the word var in a function declaration. For example, the following caused me a few hours of searching to find the cause of my blank screen: “function (var $id) { …” (error is in bold).
  • Include a dollar sign when you aren’t suppossed to. For example: “$name = $account->$Name” causes an error.
  • Not including a dollar sign when you are suppossed to. For example: “for ($i=0; i<count($myArray); $i++) { …” causes an error (missing a dollar sign before the middle i)
  • Forgot a bracket (same as above, sometimes results in error message, sometimes in blank screen)
  • Entered <?php at the beginning of a document instead of <?PHP (stupid case-sensitivity…)

Parse error: parse error, unexpected T_ELSE – In writing an if/else structure, you forgot to include the closing bracket } between the end of if and the beginning of else

Parse error: parse error, unexpected ‘=’ – You forgot to include a $ in front of a variable when making a value assignment. For example: “i = 5;” instead of “$i = 5;”