7个超级实用的PHP代码片段分享(1)_PHP教程

WBOY
풀어 주다: 2016-07-20 10:57:54
원래의
766명이 탐색했습니다.

1、超级简单的页面缓存

如果你的工程项目不是基于 CMS 系统或框架,打造一个简单的缓存系统将会非常实在。下面的代码很简单,但是对小网站而言能切切实实解决问题。

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

点击这里查看详细情况:http://wesbos.com/simple-php-page-caching-technique/

2、在 PHP 中计算距离

这是一个非常有用的距离计算函数,利用纬度和经度计算从 A 地点到 B 地点的距离。该函数可以返回英里,公里,海里三种单位类型的距离。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> distance(</span><span class="vars">$lat1</span><span>, </span><span class="vars">$lon1</span><span>, </span><span class="vars">$lat2</span><span>, </span><span class="vars">$lon2</span><span>, </span><span class="vars">$unit</span><span>) {   </span></span></li>
<li><span> </span></li>
<li class="alt">
<span>  </span><span class="vars">$theta</span><span> = </span><span class="vars">$lon1</span><span> - </span><span class="vars">$lon2</span><span>;  </span>
</li>
<li>
<span>  </span><span class="vars">$dist</span><span> = sin(</span><span class="func">deg2rad</span><span>(</span><span class="vars">$lat1</span><span>)) * sin(</span><span class="func">deg2rad</span><span>(</span><span class="vars">$lat2</span><span>)) +  </span><span class="func">cos</span><span>(</span><span class="func">deg2rad</span><span>(</span><span class="vars">$lat1</span><span>)) * </span><span class="func">cos</span><span>(</span><span class="func">deg2rad</span><span>(</span><span class="vars">$lat2</span><span>)) * </span><span class="func">cos</span><span>(</span><span class="func">deg2rad</span><span>(</span><span class="vars">$theta</span><span>));  </span>
</li>
<li class="alt">
<span>  </span><span class="vars">$dist</span><span> = </span><span class="func">acos</span><span>(</span><span class="vars">$dist</span><span>);  </span>
</li>
<li>
<span>  </span><span class="vars">$dist</span><span> = rad2deg(</span><span class="vars">$dist</span><span>);  </span>
</li>
<li class="alt">
<span>  </span><span class="vars">$miles</span><span> = </span><span class="vars">$dist</span><span> * 60 * 1.1515;  </span>
</li>
<li>
<span>  </span><span class="vars">$unit</span><span> = </span><span class="func">strtoupper</span><span>(</span><span class="vars">$unit</span><span>);  </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span>  </span><span class="keyword">if</span><span> (</span><span class="vars">$unit</span><span> == </span><span class="string">"K"</span><span>) {  </span>
</li>
<li class="alt">
<span>    </span><span class="keyword">return</span><span> (</span><span class="vars">$miles</span><span> * 1.609344);  </span>
</li>
<li>
<span>  } </span><span class="keyword">else</span><span> </span><span class="keyword">if</span><span> (</span><span class="vars">$unit</span><span> == </span><span class="string">"N"</span><span>) {  </span>
</li>
<li class="alt">
<span>      </span><span class="keyword">return</span><span> (</span><span class="vars">$miles</span><span> * 0.8684);  </span>
</li>
<li>
<span>    } </span><span class="keyword">else</span><span> {  </span>
</li>
<li class="alt">
<span>        </span><span class="keyword">return</span><span> </span><span class="vars">$miles</span><span>;  </span>
</li>
<li><span>      }  </span></li>
<li class="alt"><span>} </span></li>
</ol>
로그인 후 복사

使用方法:

<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> distance(32.9697, -96.80322, 29.46786, -98.53506, </span><span class="string">"k"</span><span>).</span><span class="string">" kilometers"</span><span>; </span></span></li></ol>
로그인 후 복사

点击这里查看详细情况:http://www.phpsnippets.info/calculate-distances-in-php

3、将秒数转换为时间(年、月、日、小时…)

这个有用的函数能将秒数表示的事件转换为年、月、日、小时等时间格式。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> Sec2Time(</span><span class="vars">$time</span><span>){  </span></span></li>
<li>
<span>  </span><span class="keyword">if</span><span>(</span><span class="func">is_numeric</span><span>(</span><span class="vars">$time</span><span>)){  </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$value</span><span> = </span><span class="keyword">array</span><span>(  </span>
</li>
<li>
<span>      </span><span class="string">"years"</span><span> => 0, </span><span class="string">"days"</span><span> => 0, </span><span class="string">"hours"</span><span> => 0,  </span>
</li>
<li class="alt">
<span>      </span><span class="string">"minutes"</span><span> => 0, </span><span class="string">"seconds"</span><span> => 0,  </span>
</li>
<li><span>    );  </span></li>
<li class="alt">
<span>    </span><span class="keyword">if</span><span>(</span><span class="vars">$time</span><span> >= 31556926){  </span>
</li>
<li>
<span>      </span><span class="vars">$value</span><span>[</span><span class="string">"years"</span><span>] = </span><span class="func">floor</span><span>(</span><span class="vars">$time</span><span>/31556926);  </span>
</li>
<li class="alt">
<span>      </span><span class="vars">$time</span><span> = (</span><span class="vars">$time</span><span>%31556926);  </span>
</li>
<li><span>    }  </span></li>
<li class="alt">
<span>    </span><span class="keyword">if</span><span>(</span><span class="vars">$time</span><span> >= 86400){  </span>
</li>
<li>
<span>      </span><span class="vars">$value</span><span>[</span><span class="string">"days"</span><span>] = </span><span class="func">floor</span><span>(</span><span class="vars">$time</span><span>/86400);  </span>
</li>
<li class="alt">
<span>      </span><span class="vars">$time</span><span> = (</span><span class="vars">$time</span><span>%86400);  </span>
</li>
<li><span>    }  </span></li>
<li class="alt">
<span>    </span><span class="keyword">if</span><span>(</span><span class="vars">$time</span><span> >= 3600){  </span>
</li>
<li>
<span>      </span><span class="vars">$value</span><span>[</span><span class="string">"hours"</span><span>] = </span><span class="func">floor</span><span>(</span><span class="vars">$time</span><span>/3600);  </span>
</li>
<li class="alt">
<span>      </span><span class="vars">$time</span><span> = (</span><span class="vars">$time</span><span>%3600);  </span>
</li>
<li><span>    }  </span></li>
<li class="alt">
<span>    </span><span class="keyword">if</span><span>(</span><span class="vars">$time</span><span> >= 60){  </span>
</li>
<li>
<span>      </span><span class="vars">$value</span><span>[</span><span class="string">"minutes"</span><span>] = </span><span class="func">floor</span><span>(</span><span class="vars">$time</span><span>/60);  </span>
</li>
<li class="alt">
<span>      </span><span class="vars">$time</span><span> = (</span><span class="vars">$time</span><span>%60);  </span>
</li>
<li><span>    }  </span></li>
<li class="alt">
<span>    </span><span class="vars">$value</span><span>[</span><span class="string">"seconds"</span><span>] = </span><span class="func">floor</span><span>(</span><span class="vars">$time</span><span>);  </span>
</li>
<li>
<span>    </span><span class="keyword">return</span><span> (</span><span class="keyword">array</span><span>) </span><span class="vars">$value</span><span>;  </span>
</li>
<li class="alt">
<span>  }</span><span class="keyword">else</span><span>{  </span>
</li>
<li>
<span>    </span><span class="keyword">return</span><span> (bool) FALSE;  </span>
</li>
<li class="alt"><span>  }  </span></li>
<li><span>} </span></li>
</ol>
로그인 후 복사

点击这里查看详细情况:http://ckorp.net/sec2time.php

1

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445736.htmlTechArticle1、超级简单的页面缓存 如果你的工程项目不是基于 CMS 系统或框架,打造一个简单的缓存系统将会非常实在。下面的代码很简单,但是对小...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿