ホームページ >バックエンド開発 >PHPチュートリアル >共有する 7 つの超実践的な PHP コード スニペット (1)_PHP チュートリアル

共有する 7 つの超実践的な PHP コード スニペット (1)_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-20 10:57:54802ブラウズ

1. 非常に単純なページのキャッシュ

プロジェクトが CMS システムまたはフレームワークに基づいていない場合は、単純なキャッシュ システムを構築するのが非常に実用的です。以下のコードは非常に単純ですが、実際には小規模な Web サイトの問題を解決できます。

<ol class="dp-c">
<li class="alt"><span><span><?php  </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><span class="func">filemtime</span><span>(</span><span class="vars">$cachefile</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></span></li>
<li><span>    <html>  </span></li>
<li class="alt"><span>        output all your html here.  </span></li>
<li><span>    </html>  </span></li>
<li class="alt"><span><?php  </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までの距離を計算します。この関数は、マイル、キロメートル、海里の 3 つの単位タイプで距離を返すことができます。

<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.com本当http://www.bkjia.com/PHPjc/445736.html技術記事 1. 非常に単純なページ キャッシュ プロジェクトが CMS システムまたはフレームワークに基づいていない場合は、単純なキャッシュ システムを構築するのが非常に実用的です。以下のコードはシンプルですが、ちょっとした用途には役立ちます...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。