Home > Article > Backend Development > 7 super practical PHP code snippets to share (1)_PHP Tutorial
1. Super simple page caching
If your project is not based on a CMS system or framework, building a simple caching system will be very practical. The code below is very simple, but it can actually solve the problem for small websites.
<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>
Click here for details: http://wesbos.com/simple-php-page-caching-technique/
2. Calculate distance in PHP
This is a very useful distance calculation function that uses latitude and longitude to calculate the distance from point A to point B. This function can return distance in three unit types: miles, kilometers, and nautical miles.
<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>
Usage:
<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>
Click here for details: http://www.phpsnippets.info/calculate-distances-in-php
3. Convert seconds to time (year, month, day, hour...)
This useful function can convert events represented by seconds into years. , month, day, hour and other time formats.
<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>
Click here for details: http://ckorp.net/sec2time.php
1