Seven Super Practical PHP code snippets _ PHP tutorials

Source: Internet
Author: User
Seven Super Practical PHP code snippets. It is a good programmer to obtain the key code. In this article, the mango site collects some key code such as this for programming. 1. it is a good programmer to obtain the key code when the key page is slow. In this article, the mango site collects some key code such as this for programming.

1. super simple page cache

If your project is not based on a CMS system or framework, creating a simple cache system will be very practical. The following code is very simple, but it can solve problems for small websites.

 output all your html here.
 

Click here to view details: http://wesbos.com/simple-php-page-caching-technique/

2. calculate distance in PHP

This is A very useful distance calculation function. it uses latitude and longitude to calculate the distance from location A to location B. This function returns the distance of three units in miles, kilometers, and nautical miles.

function distance($lat1, $lon1, $lat2, $lon2, $unit) { $theta = $lon1 - $lon2;$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));$dist = acos($dist);$dist = rad2deg($dist);$miles = $dist * 60 * 1.1515;$unit = strtoupper($unit);if ($unit == "K") {return ($miles * 1.609344);} else if ($unit == "N") {return ($miles * 0.8684);} else {return $miles;}}

Usage:

echo distance(32.9697, -96.80322, 29.46786, -98.53506, "k")." kilometers";

Click here to view details: http://www.phpsnippets.info/calculate-distances-in-php

3. convert the number of seconds to the time (year, month, day, hour ...)

This useful function converts an event in seconds to a time format such as year, month, day, and hour.

function Sec2Time($time){if(is_numeric($time)){$value = array("years" => 0, "days" => 0, "hours" => 0,"minutes" => 0, "seconds" => 0,);if($time >= 31556926){$value["years"] = floor($time/31556926);$time = ($time%31556926);}if($time >= 86400){$value["days"] = floor($time/86400);$time = ($time%86400);}if($time >= 3600){$value["hours"] = floor($time/3600);$time = ($time%3600);}if($time >= 60){$value["minutes"] = floor($time/60);$time = ($time%60);}$value["seconds"] = floor($time);return (array) $value;}else{return (bool) FALSE;}}

Click here to view details: http://ckorp.net/sec2time.php

4. Force File Download

Some mp3 files are usually played or used directly in the client browser. It's okay if you want them to be forcibly downloaded. You can use the following code:

function downloadFile($file){$file_name = $file;$mime = 'application/force-download';  header('Pragma: public');   // required  header('Expires: 0');  // no cache  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  header('Cache-Control: private',false);  header('Content-Type: '.$mime);  header('Content-Disposition: attachment; filename="'.basename($file_name).'"');  header('Content-Transfer-Encoding: binary');  header('Connection: close');  readfile($file_name);  // push it out  exit();}

Click here to view details: Credit: Alessio Delmonti

5. Use Google API to obtain current weather information

Want to know the weather today? This code will tell you that only three lines of code are required. You only need to replace the ADDRESS with the desired city.

$xml = simplexml_load_file('http://www.google.com/ig/api?weather=ADDRESS');$information = $xml->xpath("/xml_api_reply/weather/current_conditions/condition");echo $information[0]->attributes();

Click here to view details: http://ortanotes.tumblr.com/post/200469319/current-weather-in-3-lines-of-php

6. obtain the longitude and latitude of an address

With the popularity of Google Maps APIs, developers often need to obtain the longitude and latitude of a specific location. This very useful function uses an address as a parameter and returns an array containing longitude and latitude data.

___FCKpd___6

Click here to view details: http://snipplr.com/view.php? Codeview/id = 47806

7. use PHP and Google to get the favicon of the domain name

Some websites or Web applications need to use the favicon from other websites. Google and PHP can be easily used, but the premise is that Google will not be reset!

function get_favicon($url){$url = str_replace("http://",'',$url);return "http://www.google.com/s2/favicons?domain=".$url;}

Click here to view details: http://snipplr.com/view.php? Codeview/id = 45928

Reference: 10 super useful PHP snippets

Bytes. In this article, the mango site collects some key code such as this for programming. 1. super simple page easing...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.