10 most useful PHP inbuilt functions

PHP is one of the most admired and popular server side scripting language. It has become a preferred choice of the website developers. Even after using PHP for years, you may not familiar with the few functions, which can be quite useful in your development process. Today, we are going to find 10 of them. So, let's start-

1. highlight_string()

This function is use to your source code as string in your web page. It return a string with syntax highlighted like html tags, php syntax, inline css code, color, font etc. Try this -
highlight_string('<?php phpinfo(); ?>');

2. escapeshellcmd()

It is a very useful function to protect your server from shell attack. It escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. Any data that coming from user input is escaped using this function before the data is passed to any executable of system function.
$cmd = 'path; rm -rf /';
$escapedCmd = escapeshellcmd($cmd);

3. trim()

It uses to remove white space from the start and end of a string. The characters it strips by default are newlines and carriage returns , horizontal and vertical tabs, end-of-string characters, and spaces.
$str = ' Hello world ';
echo trim($str); // Hello World 
echo trim($str, 'H' ); // ello World

4. strtolower()

This is a very useful string function to convert an upper case or title case string to lower case. This function is binary-safe and can be used for comparing strings.
$str = "HeLlO WOrLD";
echo strtolower($str); // hello world 

5. strtoupper()

It is totally opposite to strtolower(), covert mixed case string to all upper case.
$str = "hello World";
echo strtolower($str); // HELLO WORLD

6. levenshtein()

To find out distance between two strings, levenshtein() function used. Its a simplest form of function that take only the two strings as parameter and returns the distance in number. Sometime its return -1, if one of the argument strings is longer than the limit of 255 characters. 
$str1 = "apple";
$str2 = "appplee";
echo levenshtein($str1, $str2); // 2



7. get_defined_vars()

It returns a multidimensional array with a list of defined variables.
print_r(get_defined_vars());

8. htmlspecialchars()

This function convert all the special characters in the string to HTML entities. If the input string passed and the return the same character set, this function is sufficient to prepare input for inclusion in most contexts of an HTML document.
htmlspecialchars("<span>Testing</span>", ENT_QUOTES);
// &lt;span&gt;Testing&lt;/span&gt;

9. checkdate()

This function check the given data is valid or not and return true or false. A date is considered valid if each parameter is properly defined (m, d, y formation).
var_dump(checkdate(12, 31, 2016)); // true
var_dump(checkdate(22, 15, 2017)); // false

10. function_exists()

It uses to check the function name may exist or not, both built-in and user-defined.
echo function_exists('isset'); // true
echo function_exists('foo'); // false

Comments