Like many other programming languages, PHP also has built-in functions that many don't know about. These functions though can be quite useful and may be the answers to questions that you've been asking for a long time. Check them out! |
1. highlight_string(): This function is very useful when you’re displaying PHP code on a website. It gives you a syntax highlighted version of the PHP code using colours defined in the highlighter, which is built-in for PHP.
highlight_string('');
?>
2. str_word_count(): You put a string as a parameter and this function gives you a word count.
?php
$str = "How many words do I have?";
echo str_word_count($str); //Outputs 5
?>
3. levenshtein(): This function is very useful in finding the typos committed by users. It differentiates between two words that are very similar in their spellings.
$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2
?>
4. get_defined_vars(): This function is useful for debugging your PHP code. It will return a multidimensional array that will contain a list of all defined variables.
print_r(get_defined_vars());
5. escapeshellcmd(): This command escapes characters in a string that may be used in order to trick your shell command to execute arbitrary commands. It can be used in order to ensure that data from users should be escaped before it is passed to functions like system() and exe().
$command = './configure '.$_POST['configure_options'];
$escaped_command = escapeshellcmd($command);
system($escaped_command);
?>
6. checkdate(): This function is used in order to check the validity of dates that are formed by the arguments. You can use this to test user submitted dates.
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
//Output
//bool(true)
//bool(false)
?>
7. php_strip_whitespace(): This command will give you the source code in filename along with PHP comments and whitespaces removed from it. It is the same as using php –w on the Command Line.
// PHP comment here
/*
* Another PHP comment
*/
echo php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:
do_nothing();
?>