7個超級實用的PHP程式碼片段分享(1)_PHP教程

來源:互聯網
上載者:User
1、超級簡單的頁面緩衝

如果你的工程項目不是基於 CMS 系統或架構,打造一個簡單的緩衝系統將會非常實在。下面的代碼很簡單,但是對小網站而言能切切實實解決問題。

 
  1. // define the path and name of cached file
  2. $cachefile = 'cached-files/'.date('M-d-Y').'.php';
  3. // define how long we want to keep the file in seconds. I set mine to 5 hours.
  4. $cachetime = 18000;
  5. // Check if the cached file is still fresh. If it is, serve it up and exit.
  6. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
  7. include($cachefile);
  8. exit;
  9. }
  10. // if there is either no file OR the file to too old, render the page and capture the HTML.
  11. ob_start();
  12. ?>
  13. output all your html here.
  14. // We're done! Save the cached content to a file
  15. $fp = fopen($cachefile, 'w');
  16. fwrite($fp, ob_get_contents());
  17. fclose($fp);
  18. // finally send browser output
  19. ob_end_flush();
  20. ?>

點擊這裡查看詳細情況:http://wesbos.com/simple-php-page-caching-technique/

2、在 PHP 中計算距離

這是一個非常有用的距離計算函數,利用緯度和經度計算從 A 地點到 B 地點的距離。該函數可以返回英裡,公裡,海裡三種單位類型的距離。

 
  1. function distance($lat1, $lon1, $lat2, $lon2, $unit) {
  2. $theta = $lon1 - $lon2;
  3. $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  4. $dist = acos($dist);
  5. $dist = rad2deg($dist);
  6. $miles = $dist * 60 * 1.1515;
  7. $unit = strtoupper($unit);
  8. if ($unit == "K") {
  9. return ($miles * 1.609344);
  10. } else if ($unit == "N") {
  11. return ($miles * 0.8684);
  12. } else {
  13. return $miles;
  14. }
  15. }

使用方法:

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

點擊這裡查看詳細情況:http://www.phpsnippets.info/calculate-distances-in-php

3、將秒數轉換為時間(年、月、日、小時…)

這個有用的函數能將秒數表示的事件轉換為年、月、日、小時等時間格式。

 
  1. function Sec2Time($time){
  2. if(is_numeric($time)){
  3. $value = array(
  4. "years" => 0, "days" => 0, "hours" => 0,
  5. "minutes" => 0, "seconds" => 0,
  6. );
  7. if($time >= 31556926){
  8. $value["years"] = floor($time/31556926);
  9. $time = ($time%31556926);
  10. }
  11. if($time >= 86400){
  12. $value["days"] = floor($time/86400);
  13. $time = ($time%86400);
  14. }
  15. if($time >= 3600){
  16. $value["hours"] = floor($time/3600);
  17. $time = ($time%3600);
  18. }
  19. if($time >= 60){
  20. $value["minutes"] = floor($time/60);
  21. $time = ($time%60);
  22. }
  23. $value["seconds"] = floor($time);
  24. return (array) $value;
  25. }else{
  26. return (bool) FALSE;
  27. }
  28. }

點擊這裡查看詳細情況:http://ckorp.net/sec2time.php

1

http://www.bkjia.com/PHPjc/445736.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445736.htmlTechArticle1、超級簡單的頁面緩衝 如果你的工程項目不是基於 CMS 系統或架構,打造一個簡單的緩衝系統將會非常實在。下面的代碼很簡單,但是對小...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.