7 little known but super useful PHP functions

by Jean. 6 Comments -

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.

highlight_string()

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.

Usage:

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

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

str_word_count()

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

Usage:

?php
$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

levenshtein()

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.

Usage:

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

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

get_defined_vars()

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.

Usage:

print_r(get_defined_vars());

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

escapeshellcmd()

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.

Usage:

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

$escaped_command = escapeshellcmd($command);
 
system($escaped_command);
?>

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

checkdate()

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.

Usage:

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

//Output
//bool(true)
//bool(false)
?>

Documentation: http://php.net/checkdate

php_strip_whitespace()

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

Usage:

<?php
// PHP comment here

/*
 * Another PHP comment
 */

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

The output:

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

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

  • http://yuriybabenko.com Yuriy Babenko

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

  • http://www.eyecreamhq.com Jamie

    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!

  • http://www.zeemo.com.au Sukhbir Mehla

    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!

  • http://kieranrushby.com Kieran Rushby

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

  • http://ReturnOnNow.com/blog/ Tommy Landry

    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!