I enjoyed PHP in a NutShell. However, much of the infromation is now dated. Here is what is still relevant.

Highlight parts of a string.

$mystr = 'var_dump($foo); ?>';

file_put_contents("highlighter.php", $mystr);

As you can see, that passes the string into highlight_string( ), then saves it out as highlighter.php and passes that filename into highlight_file( ) to print out again. Both highlight_string( ) and highlight_file( ) can take a second parameter, which, if set to true, will make these functions return the highlighted HTML rather than just print it out directly, giving you more control over it.

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.

Used for both renaming and moving files, rename( ) takes two parameters: the original filename and the new filename you wish to use. The function can rename/move files across directories and drives, and will return true on success or false otherwise.

Here is an example:

$filename2 = $filename . '.old';
$result = rename($filename, $filename2);
if ($result) {
print "$filename has been renamed to $filename2.\n";
} else {
print "Error: couldn't rename $filename to $filename2!\n";

Creating a PDF document is similar to creating a picture in that, to get the desired end result, you state the list of drawing actions required to get there—drawing lines, text, adding fonts, etc. You need to track the PDF document you are working with at all times, because other PDF functions use it.

Even creating a simple PDF takes quite a few functions; this next code block does comparatively little:

$pdf = pdf_new( );
pdf_open_file($pdf, "/path/to/your.pdf");
$font = pdf_findfont($pdf, "Times-Roman", "host");

pdf_begin_page($pdf, 595, 842);
pdf_setfont($pdf, $font, 30);
pdf_show_xy($pdf, "Printing text is easy", 50, 750);


Starting at line one, we use pdf_new( ) to create a new PDF document and store it in $pdf. This value will be used in all the subsequent functions, so it is important to keep.

The pdf_open_file( ) function is used to open a file for writing. Note that the free version of PDFlib does not allow alteration of existing PDFs; this function merely creates a new PDF of the given filename. Naturally, it will need to be somewhere your web server is able to write to; otherwise, you will receive an error along the lines of "Fatal error: PDFlib error: function 'PDF_set_info' must not be called in 'object' scope in yourscript.php on line XYZ".

The next line uses pdf_findfont( ) to find and load a font for use inside the generated PDF file. In the example, pdf_findfont( ) takes three parameters—the PDF document to work with, the name of the font to use, and which encoding to use. In the example above, $pdf is specified as the first parameter (as always). "Times-Roman" is specified as the font to use, which is one of the 14 standard internal PDFlib fonts. The next parameter can be set to either "winansi" (Windows), "macroman" (Macintosh), "ebcdic" (EBCDIC code page 1047 machines), "builtin" (for symbol fonts), or "host" (winansi for Windows, macroman for Macintosh, etc.; recommended).

When successful, pdf_findfont( ) returns a font resource which is stored in $font. You may wish to add error checking in your own scripts for extra reliability.

At this point, we're ready to start on the main part of PDF generation. The first three lines merely set things up for the document. The next four—lines four to seven—are the page itself. Reading the source, it should be easy to see that line four and line seven encapsulate one page in the generated PDF file. Objects and text outputted between a pdf_begin_page( ) and pdf_end_page( ) will affect that page, and multiple begin/end blocks are used to create multiple pages.

Note that pdf_begin_page( ) takes a second and third parameter: the X and Y point size of this page. The PDF format allows you to make your pages different point sizes from page to page, but you will most often want to choose one size and stick with it.

You need to pass three parameters to pdf_setfont( ): the first is the PDF resource, as usual; the second parameter is the return value from pdf_findfont for the font you wish to use; and the final parameter is the size to use, in points. Immediately afterward, we call pdf_show_xy( ) to place text into our page. Parameter two of pdf_show_xy( ) is the string to use, and parameters three and four are the X and Y coordinates at which to print the text.

The last parameter passed to pdf_show_xy( ) is the distance the text should appear above the page baseline in points. That is, setting this parameter to 0 will have the bottom of a lowercase "a" at the very bottom of the page, and the bottom of a lowercase "y" outside the margins of the page.

With pdf_end_page( ) called, the first and only page is completed, and all that is left to do is clean things up. This is done through the help of two functions, which are pdf_close( ) and pdf_delete( ). They may sound somewhat similar, but you do need to call them both: pdf_close( ) cleans up the PDFlib memory and document-related resources, whereas pdf_delete( ) cleans up PHP's reference to $pdf and any other internal resources. Be sure to call them in the order shown above.

When you run that script through your web browser, you won't see any "Success!" message printed out. However, you should find your PDF file has been created and is viewable in your PDF reader of choice.

Bibliographical Information

PHP in a Nutshell:A Desktop Quick Reference

By Paul Hudson

Publisher: O'Reilly Media

Release Date: June 2009

Further Reading

SVG Unleashed by Andrew Watt and Chris Lilley (Sams)

This book doesn't cover SVG. But if you want to know more about XML this is the first place to look.

Unix Shell Programming by Stephen Kochan and Patrick Wood (Sams)

General Unix and C programming is very similar to PHP, so you can learn a lot about PHP by learning about the Unix shell.
* PHP Builder (http://www.phpbuilder.com) publishes a number of high-quality PHP tutorials each year, and also has very active forums full of people ready to help.
* All the content at http://www.cookiecentral.com. is available for free, and there is also an active messageboard for you to ask questions or see what others are saying.
* Don't try to remember all the ASCII codes—you can find them online at http://www.asciitable.com.
* Finally, if all else fails and you're still hunting around, you can visit my personal website at http://www.hudzilla.org, where I keep my own brand of PHP help.
* PHP Architect http://www.phparch.com

These are notes I made after reading this book. See more book notes

Just to let you know, this page was last updated Thursday, Jan 18 18