Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Page 3 - Five Things You Didn't Know You Could Do with PHP
Five Things You Didn't Know You Could Do with PHP
By Larry Ullman

Rate This Article: Add This Article To:

Five Things You Didn't Know You Could Do with PHP - ' Tips 4 '
( Page 3 of 3 )

- 5">

4. Quickly Searching a Directory

If you've been programming with PHP for a decent amount of time, you've probably had occasion to peruse a directory or two. Maybe you built an online photo library and needed to dynamically find every image available. Or maybe you stored data in a series of text files.

ADVERTISEMENT

In any case, you had to use the opendir() function, then use readdir() in a loop, and then finish with closedir(). In the loop itself, you most likely had to use a string or regular expression function to ensure that each file in the directory matched specific criteria. Well, I'm here to say that there's an easier way!

This little function definitely slipped through the cracks for me. Added in PHP 4.3, glob() will find files that match a given pattern. It does all of that opendir() stuff in one fell swoop. For example, if you want to link to every image in a directory, you could do this:

<?php
$dir = './path/to/directory';

// Retrieve all the images.
$files = glob ("$dir/{*.jpg,*.jpeg,*.gif}",  GLOB_BRACE);

// Confirm that matches were made.
if (is_array($files)) { 

	// Loop through the array.
	foreach ($files as $image) {
		echo "$image
\n"; } } else { echo 'No files matched the search pattern!'; } ?>

The GLOB_BRACE constant is a flag telling the function to match any of the names within the curly braces (*.jpg,*.jpeg,*.gif). There are other constants you can use for: adding slashes to the returned filenames; leaving the results unsorted; or, returning only directory names.

5. Tidying Up Your HTML

HTML Tidy is a little known-gem of a utility for Web developers. This tool analyzes HTML pages and helps you:

  • Find HTML errors
  • Clean up the messy code created by IDEs
  • Improve the accessibility of your pages for people with special needs
  • Compress your code to improve download speeds
  • And more!

HTML Tidy has been around for ages, and support for it has increasingly been included in different applications. As of version 4.3, PHP now also supports HTML Tidy as a PECL package. As of version 5, the HTML Tidy extension is built into PHP, making it even easier to use. Sadly, the whys and hows of using HTML Tidy cannot be covered here, but if I've piqued your interests, click your way over to the HTML Tidy homepage, http://tidy.sourceforge.net/, to learn more about the utility. After that, read the introductory article on installing and using HTML Tidy at Zend.com. Finally, check out the PHP Manual's list of Tidy Functions.

While there's no guarantee that you were unfamiliar with all five of the mentioned ideas, I hope that you've learned a thing or two about what PHP has to offer. Of course, I didn't intuitively figure out these tricks myself; I've learned a lot by participating in PHP newsgroups and mailing lists. It also helps to routinely read (or reread) parts of the PHP manual online. The user comments there are often great sources of inspiration and innovation.

Oh, and if anybody figures out a way that PHP can mow my lawn, please do drop me a note!

Larry Ullman is the author of several books, including PHP for the World Wide Web: Visual QuickStart Guide, PHP Advanced for the World Wide Web: Visual QuickPro Guide, and PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide. Larry also teaches PHP Programming for the Web for the University of California at Berkeley Online Extension. Larry is currently the Director of Digital Media Technology and Lead PHP Programmer at DMC Insights, Inc..

When Larry's not writing or programming, he spends his time napping and daydreaming about napping.

Read more about scripting languages! Start with The State of the Scripting Universe. Then, explore each language in more detail, by learning Five Things You Didn't Know You Could Do...

  • ...With Python
  • ...With Tcl
  • ...With Perl
  • ...With Ruby
  • Discuss this article in the DevSource forum.



     
     
    >>> More Languages Articles          >>> More By Larry Ullman
     



    Microsoft's Future: A Chat With Their CTO, Barry Briggs

    Play Video >

    All Videos >

    Julia explores the Robotics Studio!

    Read now >

    Messages to Bill Gates!

    Read now >

    View Now
    DevSource RSS FEEDS
    XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.