Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Source Highlighting

An easy way to spot very basic errors is to use a text editor that has syntax highlighting capabilities. Editors like these will recognize that you are editing a PHP script and automatically highlight the text in such a way as to make each element stand out in the source code. We discussed syntax highlighting earlier, but what I want to mention here is that PHP has built-in support for syntax highlighting itself.

The two key functions here are highlight_file() and highlight_string(), although there is also a function show_source() that is an alias to highlight_file(). This takes a filename as its parameter and outputs that file to the screen, with all keywords, strings, numbers, and functions highlighted in various colors, as shown in Figure 22-1. The highlight_string() function is almost identical, except it takes a string as its parameter.

Warning

Many people use these two functions to allow visitors to their site to view the source code for their pages. However, it is important to remember that doing so potentially reveals secret information, such as database passwords.

PHP has its own syntax highlighting system that provides a little help for debugging, but is still no replacement for full syntax highlighting

Figure 22-1. PHP has its own syntax highlighting system that provides a little help for debugging, but is still no replacement for full syntax highlighting

This example shows how to highlight a string of code and also a file:

 $mystr = '<?php $foo = "bar"; $bar = array("baz", "wombat", "foo"); var_dump($foo); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page