5 Underused PHP Functions


PHP, over the years, has evolved into a vast language with many useful functions that are overlooked. Here’s 5 functions that you have probably never used to help make your next web application easier to create.

wordwrap()

wordwrap() will, as the name suggests, wrap words. After a specified amount of characters you can separate words with characters you choose. Here’s the basic format:

wordwrap($string, $char, $separator);

Example:

$string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
echo wordwrap($string, 50, "\n");
/* Outputs:
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
*/

money_format()

money_format() is a useful function which will let you format a number to be displayed like money. The PHP manual shows the available formatting options.

Example:

$amount = 9999.99;
setlocale(LC_MONETARY, 'en_US');
echo money_format("%n", $amount); // Outputs "USD 9,999.99"
Note: money_format() does not work on Windows.

similar_text()

similar_text() allows you to figure out how similar two strings are. Here’s the way you call it:

similar_text($string1, $string2, $refvar);

$refvar will be assigned to the percent similarity of the two strings. 100 being that they’re the same and 0 being that they’re nothing alike.

Example:

similar_text("html", "tricks", $ref);
echo $ref; // Outputs 20
similar_text("html", "xml", $ref);
echo $ref; // Outputs 57.142857142857
similar_text("html", "html", $ref);
echo $ref; // Outputs 100

highlight_string()

highlight_string() provides syntax highlighting for PHP code. It’s very easy to use:

highlight_string($code);

Example:

$code = '<?php
echo $x . "hello, world!";
?>';
highlight_string($code);

This example would output the following:

<?php
echo $x "hello, world!";
?>

metaphone()

Last but not least, metaphone() takes a string and returns an approximate pronunciation of it.

Example:

echo metaphone('tricks'); // Outputs "TRKS"
echo metaphone('website'); // Outputs "WBST"

Are we missing any functions? Share any obscure but useful PHP functions that you know about in a comment below. We hope these helped you!

This entry was posted in PHP. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

13 Comments

  1. Posted July 19, 2010 at 12:05 PM | Permalink

    In your code maybe…

  2. Syaz
    Posted July 21, 2010 at 4:32 AM | Permalink

    They are underused for a reason. Unless all you do is blogging, these functions may never see the light of day.

    • Posted July 21, 2010 at 2:12 PM | Permalink

      I don’t think so. Some of them are pretty useful.
      Really just depends on what your script needs.

  3. Posted July 21, 2010 at 5:06 PM | Permalink

    I actually am curious, can you defend why you label these as underused?

    wordwrap() maybe. format_money(), sure… but metaphone()? similar_text()? How are these actually underused? What use-cases are these useful for?

    • Posted July 21, 2010 at 8:17 PM | Permalink

      similar_text() I could see being used with one of those security questions, where you could check if the answer is over 95% close than they’ll count it as correct (example).

      highlight_string() is used in most forum softwares to provide fast PHP syntax highlighting, by the way.

    • Posted July 21, 2010 at 11:19 PM | Permalink

      I’d go as far to say all forums use highlight_string(), since it requires less work to set up (as apposed to something like GeSHi, where you’d have to upload the library and then embed it).

    • Posted July 22, 2010 at 7:55 AM | Permalink

      Not all of them, just the PHP based forum systems.

    • Posted July 22, 2010 at 10:21 PM | Permalink

      Ah, quite right. My mistake.

  4. Posted August 18, 2011 at 4:52 AM | Permalink

    I like all these functions and think they should be readily available in just about every place where there is a text box! Accept on social network sites such as Facebook where the posts could become messy and clash with the uniformity of the site. Glad you posted this code here though for us who are making sites.

  5. Posted August 23, 2011 at 4:44 PM | Permalink

    Your right. I can’t remember the last time I used any of those.
    Learn something new every day, especially when I come to a programing language.
    Thanks for the info.

  6. Posted September 11, 2011 at 10:36 AM | Permalink

    My feeling is that it won’t hurt for these functions to be made more available. Even if only a few people use them, then that should make a few people happier than they would have been without them. Also, I think that you can’t really tell how many will make use of them until you give people access to them.

  7. Posted September 14, 2011 at 1:38 AM | Permalink

    Visit our site and know more about

    [url=http://www.dymatize-nutrition.pl/dymatize-super-amino.html]Super Amino[/url]

  8. Posted December 22, 2011 at 5:21 AM | Permalink

    Braided sleeving can be made from any thermoplastic material. Polyolefin and polyvinyl chloride are the most popular choices in material to create this form of plastic sleeving.

3 Trackbacks

  1. By abcphp.com on July 19, 2010 at 5:36 AM

    5 Underused PHP Functions | HTML-Tricks…

    PHP, over the years, has evolved into a vast language with many useful functions that are overlooked. Here’s 5 functions that you have probably never used to help make your next web application easier to create….

  2. By 網站製作學習誌 » [Web] 連結分享 on July 20, 2010 at 12:09 AM

    [...] 5 Underused PHP Functions [...]

  3. By 5 Underused PHP Functions | theUnseen on July 20, 2010 at 7:08 AM

    [...] http://html-tricks.com/php/5-underused-functions/ Like Unlike share this with your people [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>