PHP or “Hypertext Preprocessor” is one of the most widely used open source scripting language designed for web development. There are millions of website built with PHP. I am a software engineer and working as a PHP developer for the last 3 years. In this article i have collected 10 best PHP code snippets for you to use in your projects. Happy Coding!

1. Encode Email Address:

The below function takes a string input (the email address), loops through each character replacing the letter with the character’s ASCII value, and returns the encoded email address.

2. Validate email Address:

To check if an email address is valid, the common solution is regular expressions. For instance, to validate an email address in PHP use the following simple code.

3. Get Real IP Address of Client:

In the above PHP function, first attempt is to get the direct IP address of client’s machine, if not available then try for forwarded for IP address using HTTP_X_FORWARDED_FOR. And if this is also not available, then finally get the IP address using REMOTE_ADDR.

4. Convert URLs within String into hyperlinks:

This is very useful function which will convert a string of text and add hyperlinks to all the URLs. We loop through each URL and add a hyperlink.

5. Random password generator:

PHP password generator is a complete, working random password generation function for PHP. It allows the developer to customize the password: set its length and strength. Just include this function anywhere in your code and then use it.

6. How to block multiple IP adresses:

It is sometimes necessary to block people from using your site, dependent on their IP address. A users IP address can be detected by PHP using the $_SERVER superglobal and the parameter REMOTE_ADDR. This PHP function is used to block user with multiple ip adresses.

7. Send a Text Message:

With the help of this PHP function you can send text messeges to your clients or whoever you want.

8. Pagination Function:

The following PHP function creates an array of navigable links which can be easily used to create  pagination. This can be used as a generic pagination function, because it accepts all necessary parameters.

9. Parse XML Data:

PHP has a class called SimpleXML which is simple to use for parsing XML data.

10. Download File Forcefully:

Provide files to the user by forcing them to download. This type of download start automatically when user clicks.