How do I put blocks of PHP code into a PHPDoc DocBlock

Mark Locker picture Mark Locker · Jul 31, 2012 · Viewed 11.9k times · Source

I'm playing around with PHPDoc and have realised that you can use markdown to add some formatting to a DocBlock. In particular, I notice that you can use back ticks to highlight inline code.

However, I can't seem to figure out how to add blocks of code to a DocBlock, as using 4 spaces doesn't seem to work.

I've tried using <code> and <pre> too, and whilst those tags do appear in the generated documentation, the code inside them becomes commented out with HTML comments.

For example, this DocBlock:

/**
 * This is a test DocBlock
 *
 * <pre>
 *     <?php
 *         echo('hi');
 *     ?>
 * </pre>
 *
 * @return object[] An array of objects.
 */

Generates this HTML:

<pre>
    <!--?php echo('hi'); ?-->
</pre>

Where am I going wrong? How can I add a block of code to my DocBlock?

Answer

Mat-Locomotive picture Mat-Locomotive · Apr 3, 2014

phpdocumentor uses the github variant of markdown.

The proper way to add code, is then:

/**
 * This is a test DocBlock
 *
 * ```php
 * echo('hi');
 * ```
 *
 * @return ...
 */