Heim > Artikel > Backend-Entwicklung > 7个超级实用的PHP代码片段分享(1)_PHP教程
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