Dynamic Web Lab
Back to insights

8 useful PHP code snippets for developers

September 10, 20123 min read

Today, i want to share you some useful PHP code snippets which may reduce your development time.

1.Send HTML Mail

<?php
$to = "[email protected]";
$subject = "This is my test html email send using php mail function";
$body    = "Body of your message here you can use HTML too. <br /> <h1> Header </h1> ;";
$headers = "From: User 
";
$headers.= "Reply-To: [email protected]
";
$headers.= "Return-Path: [email protected]
";
$headers.= <"X-Mailer: PHP5
";
$headers.= 'MIME-Version: 1.0'."
";
$headers.= 'Content-type: text/html; charset=iso-8859-1'."
";
mail($to,$subject,$body,$headers);
?>

2.Detect Browser using PHP function

<?php
echo $_SERVER['HTTP_USER_AGENT'] . "

";
$browser = get_browser(null, true);
print_r($browser);

// sample output

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    [cssversion] => 2
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [backgroundsounds] =>
    [vbscript] =>
    [javascript] => 1
    [javaapplets] => 1
    [activexcontrols] =>
    [cdf] =>
    [aol] =>
    [beta] => 1
    [win16] =>
    [crawler] =>
    [stripper] =>
    [wap] =>
    [netclr] =>
)

?>

3. Get Remote IP address

echo $_SERVER['REMOTE_ADDR'];
//sample output 127.0.0.1

4. Read CSV file with PHP

<?php
function getCSVFile ( $CSVfile, $delimiter ) {

 if(isset($CSVfile) AND isset($delimiter) ){
 
	//Open csv file for reading.
	$handleCSV = fopen($CSVfile, "r");
	
	if($handleCSV) {
	
	//Read each line and print the line out.
	while (($line_array = fgetcsv($handleCSV, 4000, $delimiter)) !== false) {
 
		//Do something to each line.
		echo "<pre>";
		print_r($line_array);
		echo "</pre>";
		}
		fclose($handleCSV);
	}
 }

}

//url of the csv file

$CSVfile = 'test.csv'; 
 
//The delimiter that is used in csv file.
$delimiter = ',';

getCSVFile ($CSVfile, $delimiter );
?>

5. Check if server is HTTPS or not

if ($_SERVER['HTTPS'] != "on") { 
	echo "This is not HTTPS";
}else{
	echo "This is HTTPS";
}

6. Get short urls for Twitter

function getTinyUrl($url) {
    return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
}

7. Simple PHP Image Crop

$file = $_FILES['image']['name'];
$newwidth = 200; // SET YOUR DESIRED WIDTH

$small_image = str_replace(" ", "_", $file);
$small_image = "small_".$small_image;
$smallfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($smallfile);
list($width,$height)=getimagesize($smallfile);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $imagepath."small/".$small_image;
imagejpeg($tmp,$filename,100);
 
imagedestroy($src);
imagedestroy($tmp);

8. Simple PHP Redirect

    function redirect($url,$die=true,$permanent=false) {
    if(headers_sent()) {
    print("<script type=\"text/javascript\">window.location.href='{$url}';</script>");
    } else {
    if($permanent) {
    header("HTTP/1.1 301 Moved Permanently");
    }
    header("Location: {$url}");
    }
    if($die) { die(); }
    }

Need Help with Your Digital Presence?

At Dynamic Web Lab, we specialize in building fast, scalable websites and applications for businesses across the UAE and beyond.

Whether you need a WordPress website, a custom Next.js application, or AI-powered solutions, our team is ready to help.

Frequently Asked Questions

Why is a professional website important for UAE businesses?

In the UAE's competitive market, a professional website serves as your digital trade license. The TDRA reports 85% of consumers research businesses online before purchasing.

How much does web development cost in Dubai?

Basic WordPress sites: AED 5,000-15,000. Custom applications: AED 25,000-50,000+. The Dubai Chamber reports 40% higher engagement with professional sites.

How does AI impact web development in 2026?

AI transforms development through automated code generation, personalized UX, and predictive analytics. UAE businesses adopting AI see 35% higher productivity.

References & Sources

Written by Dynamic Web Lab Editorial — UAE digital transformation experts since 2013.

Written by

Dynamic Web Lab Editorial

We share how we design, engineer, and scale digital products across the GCC, Europe, and the US.

Share this article

Need help implementing this?

We turn these playbooks into shipped features. Let us scope your roadmap and support your team.

Start a project conversation