7 little known but super useful PHP functions

by Jean.

PHP have lots of built-in functions, and most developers know many of them. But a few functions are not very well known, but are super useful. In this article, I have compiled little known but really cool PHP functions.


When displaying PHP code on a website, the highlight_string() function can be really helpful: It outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP.


highlight_string('<?php phpinfo(); ?>');

Documentation: http://php.net/manual/en/function.highlight-string.php


This handy function takes a string as a parameter and return the count of words, as shown in the example below.


$str = "How many words do I have?";
echo str_word_count($str); //Outputs 5

Documentation: http://php.net/manual/en/function.str-word-count.php


Ever find the need to determine how different (or similar) two words are? Then levenshtein() is just the function you need. This function can be super useful to track user submitted typos.


$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2

Documentation: http://php.net/manual/en/function.levenshtein.php


Here is a handy function when debugging: It returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.



Documentation: http://php.net/manual/en/function.get-defined-vars.php


escapeshellcmd() escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. This function should be used to make sure that any data coming from user input is escaped before this data is passed to the exec() or system() functions, or to the backtick operator.


$command = './configure '.$_POST['configure_options'];

$escaped_command = escapeshellcmd($command);

Documentation: http://php.net/manual/en/function.escapeshellcmd.php


Checks the validity of the date formed by the arguments. A date is considered valid if each parameter is properly defined. Pretty useful to test is a date submitted by an user is valid.


var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));


Documentation: http://php.net/checkdate


Returns the PHP source code in filename with PHP comments and whitespace removed. This is similar to using php -w from the commandline.


// PHP comment here

 * Another PHP comment

echo        php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:

The output:

 echo php_strip_whitespace(__FILE__); do_nothing(); ?>

Documentation: http://www.php.net/manual/en/function.php-strip-whitespace.php

CWC Special Deal!

Get one year of quality hosting + domain from A Small Orange for only $40 with our exclusive deal!

Featured WordPress Theme

Kara - Simple Portfolio Wordpress Theme

  • Didn’t know about highlight_string() and levenshtein() – great stuff, thanks for sharing!

  • I did not know much of these but it is only through this post that I was introduced to these php functions. Great job!

  • Jamie

    These PHP functions deserve to be recognized! They are basic and certainly useful!

  • Wow, I had no idea about many of these PHP functions and I have been working as a web developer for years! It’s amazing how people settle on doing things the long way when there is a much easier (and faster) way of doing it if you only do a bit of research!

    Thanks for sharing!

  • Awesome, I will definitely be using these!! Especially levenshtein(). Cheers

  • Good tips, all new to me. I’ll share this with our dev team, because I doubt they know all 7. Of course, they often surprise me, so who knows…they might already know all these and more! They’ll like your blog, it’s more technical than the stuff I share with them typically. Cheers!