Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

Guidelines for Including Code in Comments

The problem

It's great that users of our site want to share alternative solutions, However, having the solution visible in the comments below the exercise defeats the purpose of the site. The whole idea is to give people exercises to practice what they know about PHP without any outside input.

A compromise

On the Drupal 6 version of the site, we solved this problem using the syntax highlighter's collapse function. However, the Drupal 7 version of this function is much more difficult to use. So we decided to compromise and make sure that only the comments closest to the exercises showed no code or had code collapsed. Comments are now ordered with the oldest at the top of the list, and all these have been formatted to hide code.

If you don't want to see any answers before you've tried to write your own code, don't scroll down the comments!

What does this mean for me?

Here's the simple version: put your code in <code> </code> tags. For indentation, use the space bar rather than tabs. That's it!

For those who are interested, here's more detail:

On the Drupal 6 site, we required that code be enclosed in {syntaxhighlighter} tags. Those tags no longer work. Commenters now have access to two highlighting libraries. The one recommended above is the GeShi filter. You'll see it in every answer script on the site.

Alex Gorbatchev's syntax highlighting library is also still available but much more difficult to use. The tags to use it are:

  1. <pre class="brush:php, collapse:true">
  2. Some code here.
  3. </pre>

In addition, every less than character (<) in your code must be rendered as an HTML entity: &lt;. So a p tag would look like this:

More detail about both syntax highlighters is available on the Compose tips page.

The space bar vs. tabs issue for indentation pertains to the GeShi filter - tabs don't render well.

Comments that simply present the answer code will not be approved.

A word to the wise

We don't have time to test every piece of code submitted to us in comments. So don't assume that it works; be sure and test it yourself. If you find code that doesn't work, feel free to post a comment explaining what you discovered.

Thank you

We especially want to thank those of you who answer questions left by other users, and who submit working alternative solutions. (Unfortunately, some of the alternative solutions we've seen don't work. But that's another story.) So to these very helpful users: THANK YOU!!