highlight_string();

This little tool is handy for giving your PHP code some colour. It helps people distinguish between different parts of the code such as comments, variables and so on.

In category php.

Posted by Joseph Skidmore on 11.09.05.

This little tool is handy for giving your PHP code some colour. It helps people distinguish between different parts of the code such as comments, variables and so on.

I have decided to write this short tutorial to show you how to use it. When I first learnt about this I didn't know how to use it correctly and as usual took a while to get the answer. This tutorial is to save you the time and effort!

highlight_string(); code

Basically the code is just;

highlight_string();

Yup, as simple as that ..... or so I thought. Here is how to correctly use it to highlight your PHP code:

<?php highlight_string('<?php
$dbHost =; #Your db host
$dbUser =; #username
$dbPass =; #Pass
$dbname =; #Name of the database.
?>') ?> 

This shows you the results of the code, but, when you try to put more complex code into the string you will get errors. Want to know why?

<?php highlight_string('<?php
if (isset($_GET['error'])) {
  echo 'Invalid login data supplied. Please try again.';
}
?>') ?>

You need to add escape backslashes before all the ' (apostrophes) for the code to show, this is because if we did not the server would interpret this as the closing character of the code and then it would try to run the rest of the PHP code .... which of course wouldn't work.

The code actually looks like:

<?php highlight_string('<?php
if (isset($_GET['error'])) {
  echo 'Invalid login data supplied. Please try again.';
}
?>') ?>

Hopefully this will be enough information to be able to understand this small but useful piece of code.

Page generated in 0.0011 seconds.

Whatcounter Free Invisible Stats CounterFree invisible hit counter