HTML CSS JavaScript jQuery PHP Python MySQL

21 Useful PHP Snippets

Updated on by David Adams

21 Useful PHP Snippets

Looking for a list of useful PHP snippets? Look no further, simply copy and paste these useful snippets into your projects.

Remember to use the latest PHP version, some of these snippets may not work on older versions.

1. MySQL Snippets

This snippet will connect to your MySQL database:

$mysqli = mysqli_connect('localhost', 'DATABASE_USER', 'DATABASE_PASS', 'DATABASE_NAME');

Check for connection errors:

if (mysqli_connect_errno()) {
	die('Failed to connect to MySQL: ' . mysqli_connect_error());

Select database table and display all the results:

$result = $mysqli->query('SELECT * FROM students');
while ($row = $result->fetch_assoc()) {
	echo $row['name'] . '<br>';

Checking the number of rows:


Insert a new record:

$mysqli->query('INSERT INTO students (name) VALUES ("David")');

Checking the number of affected rows:


Escape special characters in a string, this should be used if you do not prepare your statements:


Prepare statement, prevents SQL injection:

$name = 'David';
$limit = 1;
$stmt = $mysqli->prepare('SELECT * FROM students WHERE name = ? LIMIT ?');
$stmt->bind_param('si', $name, $limit);
// etc...
$result = $stmt->get_result();

Close query and close database connection:


2. Escape HTML Entities

This snippet will escape HTML entities and quotes:

htmlentities($text, ENT_QUOTES, "UTF-8");

Decoding HTML entities:


3. Replace Text in String

This snippet will replace text in a string:

str_replace('Apple', 'Orange', 'My favourite fruit is an Apple.');

And to replace multiple words in a string:

str_replace(array('fruit', 'Apple'), array('Vegetable', 'Carrot'), 'My favourite fruit is an Apple.');

4. Check if a String Contains a Specific Word

This snippet will check if a string contains a specific word:

if (strpos('My name is David.', 'David') !== false) {
	// String contains David

5. Array Snippets

Inserting new items into an array:

$names = array();
array_push($names, 'David', 'John');

Remove item from an array:

// or...

Reindex the values after you have removed an item:

$names = array_values($names);

Reverse an array:

$reversed = array_reverse($names);

Merge two or more arrays:

$merged = array_merge($arr1, $arr2);

Return only the array keys:

$keys = array_keys(array('name' => 'David', 'age' => 28));

Sort an array:


Sort an array in reverse order:


Check if item exists in an array:

if (in_array('David', $names)) {
	// item exists

Check if key exists in an array:

if (array_key_exists('name', array('name' => 'David', 'age' => 28))) {
	// key exists

Count the number of items in an array:


Convert comma-separated list to array:

$array = explode(',', 'david,john,warren,tracy');

6. GET and POST Requests

Get an value from the URL parameters:

// index.php?name=david
echo $_GET['name'];

Check if request variable exists:

if (isset($_GET['name'])) {
	// exists

Send form data to PHP script, HTML file:

<form action="login.php" method="post">
	Username: <input type="text" name="username"><br>
	Password: <input type="password" name="password"><br>
	<input type="submit">


$user = $_POST['username'];
$pass = $_POST['password'];
echo 'Your username is '  . $user . ' and your password is ' . $pass . '.';

7. Create a Password Hash

This snippet will create a password hash:

$hash = password_hash('your_password', PASSWORD_DEFAULT);

Verify your hashed passwords:

if (password_verify('your_password', $hash)) {
	// hash is valid

8. Sessions

This snippet will create a new session, sessions act as cookies but are stored on the server instead of the clients computer.

$_SESSION['name'] = 'David';

Free all the session variables:


Destroy all the session variables:


9. Redirect URL

This snippet will redirect to a new page:


10. Get the Clients IP Address

This snippet will get the IP address of the client:

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
} else {
    $ip = $_SERVER['REMOTE_ADDR'];

11. Get the Contents of a URL File

This snippet will get the contents of a file using cURL:

$url = '';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($curl);

Using PHP file_get_contents:

$result = file_get_contents('file.json');

12. Determine the Date

This snippet will get the current date in string format:

$date = date('Y-m-d H:i:s');
echo $date;

Get the date from a timestamp:

$date = date('Y-m-d H:i:s', $timestamp);
echo $date;

13. JSON

This snippet will get an JSON file and decode it:

$string = file_get_contents('file.json');
$json = json_decode($string, true);
// $json['name'] etc...

Encode PHP array to JSON:

$array = array('name' => 'David', 'age' => 27);
$string = json_encode($array);
echo $string; // { "name" : "David", "age" : 27 }

14. Memcached

Connect to memcached server:

$memcached = new Memcached();
$memcached->addServer('', 11211);

Set item:

$memcached->set('name', 'David');

Get item:


15. Send Email

This snippet will send an email:

$to      = '';
$subject = 'the subject';
$message = 'hello';
$headers = 'From:' . "\r\n" . 'Reply-To:' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

16. Display the Scripts Execution Time

This snippet will display the scripts execution time:

$time_start = microtime(true); 
// Your scripts here...
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);

17. Get the Full URL

This snippet will get the full URL of your website (replace HTTP with HTTPS if your site is SSL):

$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

18. Format Numbers

This snippet will format your number with a comma:

$num = number_format(1000); 
echo $num; // 1,000

Format number with decimals:

$num = number_format(1000.12, 2); 
echo $num; // 1,000.12

19. Unzip a File

This snippet will unzip a zipped file on your server:

$zip = new ZipArchive;
$file = $zip->open('');
if ($file) {
	echo 'Archive extracted!';

20. Detect Mobile Device

This snippet will detect if the client is using a mobile device:

if (preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"])) {
	// Is mobile...

21. Check if File Exists

This snippet will check if a file exists:

if (file_exists('example.png')) {
	// File exists


These snippets are useful if you develop PHP applications regularly, or you are new and looking for a specific snippet, keep the page bookmarked for future reference.

Share, like, and follow our social media accounts if you enjoy our articles! Happy coding!