最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

超級(jí)實(shí)用的7個(gè)PHP代碼片段分享

 更新時(shí)間:2012年01月05日 12:06:34   作者:  
關(guān)鍵的時(shí)候能拿得出關(guān)鍵代碼才是好的程序員。這篇文章里收集了一些諸如此類的關(guān)鍵代碼,有用于編程
1、超級(jí)簡(jiǎn)單的頁(yè)面緩存
如果你的工程項(xiàng)目不是基于 CMS 系統(tǒng)或框架,打造一個(gè)簡(jiǎn)單的緩存系統(tǒng)將會(huì)非常實(shí)在。下面的代碼很簡(jiǎn)單,但是對(duì)小網(wǎng)站而言能切切實(shí)實(shí)解決問題。
復(fù)制代碼 代碼如下:

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

點(diǎn)擊這里查看詳細(xì)情況:http://wesbos.com/simple-php-page-caching-technique/

2、在 PHP 中計(jì)算距離
這是一個(gè)非常有用的距離計(jì)算函數(shù),利用緯度和經(jīng)度計(jì)算從 A 地點(diǎn)到 B 地點(diǎn)的距離。該函數(shù)可以返回英里,公里,海里三種單位類型的距離。
復(fù)制代碼 代碼如下:

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;
}
}

使用方法:
復(fù)制代碼 代碼如下:

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

點(diǎn)擊這里查看詳細(xì)情況:http://www.phpsnippets.info/calculate-distances-in-php

3、將秒數(shù)轉(zhuǎn)換為時(shí)間(年、月、日、小時(shí)…)
這個(gè)有用的函數(shù)能將秒數(shù)表示的事件轉(zhuǎn)換為年、月、日、小時(shí)等時(shí)間格式。
復(fù)制代碼 代碼如下:

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;
}
}

點(diǎn)擊這里查看詳細(xì)情況:http://ckorp.net/sec2time.php

4、強(qiáng)制下載文件
一些諸如 mp3 類型的文件,通常會(huì)在客戶端瀏覽器中直接被播放或使用。如果你希望它們強(qiáng)制被下載,也沒問題??梢允褂靡韵麓a:
復(fù)制代碼 代碼如下:

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();
}

點(diǎn)擊這里查看詳細(xì)情況:Credit: Alessio Delmonti

5、使用 Google API 獲取當(dāng)前天氣信息
想知道今天的天氣?這段代碼會(huì)告訴你,只需 3 行代碼。你只需要把其中的 ADDRESS 換成你期望的城市。
復(fù)制代碼 代碼如下:

$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();

點(diǎn)擊這里查看詳細(xì)情況:http://ortanotes.tumblr.com/post/200469319/current-weather-in-3-lines-of-php

6、獲得某個(gè)地址的經(jīng)緯度
隨著 Google Maps API 的普及,開發(fā)人員常常需要獲得某一特定地點(diǎn)的經(jīng)度和緯度。這個(gè)非常有用的函數(shù)以某一地址作為參數(shù),返回一個(gè)數(shù)組,包含經(jīng)度和緯度數(shù)據(jù)。
復(fù)制代碼 代碼如下:

function getLatLong($address){
if (!is_string($address))die("All Addresses must be passed as a string");
$_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
$_result = false;
if($_result = file_get_contents($_url)) {
if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
$_coords['lat'] = $_match[1];
$_coords['long'] = $_match[2];
}
return $_coords;
}

點(diǎn)擊這里查看詳細(xì)情況:http://snipplr.com/view.php?codeview&id=47806

7、使用 PHP 和 Google 獲取域名的 favicon 圖標(biāo)
有些網(wǎng)站或 Web 應(yīng)用程序需要使用來自其他網(wǎng)站的 favicon 圖標(biāo)。利用 Google 和 PHP 很容易就能搞定,不過前提是 Google 不會(huì)連接被重置哦!
復(fù)制代碼 代碼如下:

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

點(diǎn)擊這里查看詳細(xì)情況:http://snipplr.com/view.php?codeview&id=45928

相關(guān)文章

  • php的curl封裝類用法實(shí)例

    php的curl封裝類用法實(shí)例

    這篇文章主要介紹了php的curl封裝類用法,以實(shí)例形式較為詳細(xì)的講述了curl封裝類及其使用方法,并總結(jié)了GET與POST的用法,需要的朋友可以參考下
    2014-11-11
  • php圖形jpgraph操作實(shí)例分析

    php圖形jpgraph操作實(shí)例分析

    這篇文章主要介紹了php圖形jpgraph操作,結(jié)合具體實(shí)例形式分析了php基于jpgraph圖形庫(kù)實(shí)現(xiàn)圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • php中加密解密DES類的簡(jiǎn)單使用方法示例

    php中加密解密DES類的簡(jiǎn)單使用方法示例

    這篇文章主要介紹了php中加密解密DES類的簡(jiǎn)單使用方法,結(jié)合實(shí)例形式分析了php中加密解密DES類的基本定義與使用方法,需要的朋友可以參考下
    2020-03-03
  • PHP優(yōu)化之批量操作MySQL實(shí)例分析

    PHP優(yōu)化之批量操作MySQL實(shí)例分析

    這篇文章主要介紹了PHP優(yōu)化之批量操作MySQL,結(jié)合實(shí)例形式對(duì)比分析了PHP批量操作MySQL相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • 解決PHP在DOS命令行下卻無(wú)法鏈接MySQL的技術(shù)筆記

    解決PHP在DOS命令行下卻無(wú)法鏈接MySQL的技術(shù)筆記

    前段時(shí)間,由于要用 php 進(jìn)行 Shell 編程時(shí),碰到了 PHP 在 WEB 下可以連接 MySQL 而在 DOS COMMAND 命令行下卻連接失敗的問題。
    2010-12-12
  • php基于環(huán)形鏈表解決約瑟夫環(huán)問題示例

    php基于環(huán)形鏈表解決約瑟夫環(huán)問題示例

    這篇文章主要介紹了php基于環(huán)形鏈表解決約瑟夫環(huán)問題,結(jié)合具體實(shí)例形式分析了php環(huán)形鏈表的定義及基于環(huán)形鏈表解決約瑟夫環(huán)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • PHP與JavaScript針對(duì)Cookie的讀寫、交互操作方法詳解

    PHP與JavaScript針對(duì)Cookie的讀寫、交互操作方法詳解

    這篇文章主要介紹了PHP與JavaScript針對(duì)Cookie的讀寫、交互操作方法,結(jié)合實(shí)例形式分析了php與javascript設(shè)置cookie、php讀取php及js設(shè)置的cookie、js讀取php及js設(shè)置的cookie等相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • PHP下操作Linux消息隊(duì)列完成進(jìn)程間通信的方法

    PHP下操作Linux消息隊(duì)列完成進(jìn)程間通信的方法

    當(dāng)我們開發(fā)的系統(tǒng)需要使用多進(jìn)程方式運(yùn)行時(shí),進(jìn)程間通信便成了至關(guān)重要的環(huán)節(jié)。消息隊(duì)列(message queue)是Linux系統(tǒng)進(jìn)程間通信的一種方式。
    2010-07-07
  • PHP實(shí)現(xiàn)動(dòng)態(tài)獲取函數(shù)參數(shù)的方法示例

    PHP實(shí)現(xiàn)動(dòng)態(tài)獲取函數(shù)參數(shù)的方法示例

    這篇文章主要介紹了PHP實(shí)現(xiàn)動(dòng)態(tài)獲取函數(shù)參數(shù)的方法,結(jié)合實(shí)例形式分析了php針對(duì)函數(shù)參數(shù)操作func_num_args()、func_get_arg()及func_get_args()函數(shù)相關(guān)使用技巧,需要的朋友可以參考下
    2018-04-04
  • Mac下php 5升級(jí)到php 7的步驟詳解

    Mac下php 5升級(jí)到php 7的步驟詳解

    這篇文章主要給大家介紹了在Mac下將php 5升級(jí)到php 7的步驟,文中將步驟介紹的非常詳細(xì),并分享了在升級(jí)過程中可能遇到的問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-04-04

最新評(píng)論

佳木斯市| 大悟县| 固原市| 金乡县| 延边| 盖州市| 山西省| 法库县| 神农架林区| 连江县| 南乐县| 崇阳县| 广元市| 丹东市| 富阳市| 彝良县| 泽州县| 新郑市| 自治县| 西藏| 石景山区| 仙居县| 靖西县| 金沙县| 垣曲县| 新源县| 育儿| 兴海县| 万源市| 化德县| 当雄县| 攀枝花市| 林周县| 仪征市| 黎平县| 阿拉善右旗| 洞头县| 会东县| 延边| 泰顺县| 阿拉善盟|