Problem with heredoc

When using variables inside a heredoc php will parse these and puts their values there. The variables $dbms, $dbhost and all the others were not defined in this case and thus php outputs empty strings.

Instead of a heredoc we should use a nowdoc which is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.

A nowdoc is identified with the same <<< seqeuence used for heredocs, but the identifier which follows is enclosed in single quotes <<<’CONFIG’.

highlight_string

Outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP. It's important that the second argument is defined. This is a boolean argument which defaults to false inside the highlight_string() function. This argument specifies whether to output the string directly or to return the formatted code as a string.

Outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP. The second argument specifies whether to output the formatted code directly or to return it as a string. By default this argument is set to false which makes the function output the formatted code directly.

When you ommit this argument you shouldn't use echo to print the formatted code as this would lead to the additional output of a '1' otherwise.

...
highlight_string($php_string);
...

Now let's have a look at the actual output of this function. This is not the browser's page source but what we see in the browser window itself.

Many servers are configured to automatically highlight files with a phps extension. For example, example.phps when viewed will show the syntax highlighted source of the file. To enable this, add this line to the httpd.conf:

AddType application/x-httpd-php-source .phps

2 Comments

Inyavic

Posted February 27, 2014 at 8:49 AM

This is a really nice tutorial on Displaying PHP code in a html code section. I’ve had some more looks at your other articles about programming but also the Linux stuff and already learnt a couple of new things since I’m yet a beginner in these fields. Expecting more from you.