Thu Apr 11 2019

The most useful PHP inbuilt functions

Programming2287 views

The most useful PHP inbuilt functions

PHP is one of the most admired and popular server-side scripting languages. It has become a preferred choice of website developers. Even after using PHP for years, you may not be familiar with the few functions, which can be quite useful in your development process.

A function is a block of code written in a program to perform some specific task. Functions take information as parameters, executes a block of statements or perform operations on those parameters and return the result. PHP provides two major types of functions:

Built-in functions - PHP provides a huge collection of built-in library functions. These functions are already coded and stored in the form of functions. To use those you just need to call them as per your requirement like var_dump, fopen(), print_r(), gettype() and so on.

User Defined Functions - Apart from the built-in functions, PHP also allows you to create your own customized functions called the user-defined functions. By using this you can create our own packages of code and use it wherever necessary by simply calling it.

Today, we are going to find the most useful of them. So, let's start-

1. highlight_string()

This function is used to your source code as a string in your web page. It returns 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 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 in 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(), convert mixed case string to all upper case.

$str = "hello World";

echo strtolower($str); // HELLO WORLD

6. levenshtein()

To find out the distance between two strings, levenshtein() function used. It's the simplest form of a function that takes only the two strings as a parameter and returns the distance in number. Sometimes its return -1, if one of the argument string 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 converts 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 checks 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

11. register_shutdown_function()

There is a function called register_shutdown_function(), which will let you execute some code right before the script finishes running. When you use register_shutdown_function(), your code will execute no matter why the script has stopped running.

12. str_replace()

Find and replace functionality is super useful with strings. You can use find and replace for almost anything your imagination can think of.

13. strstr()

The function strstr() can be used to find a string or character match within a longer string. This function can be used to find a string inside a string, including finding a string containing only a single character.

14. php_strip_whitespace()

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

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.