background preloader

Tips

Facebook Twitter

10 super useful PHP snippets you probably haven’t seen. Text messaging with PHP using the TextMagic API If for some reason, you need to send text messages to your clients cell phones, you should definitely have a look to TextMagic. They provide an easy API which allow you to send SMS to cell phones. Please note that the TextMagic service isn’t free. The example below shows how easy it is to send a SMS to a cell phone using the TextMagic API: // Include the TextMagic PHP lib require('textmagic-sms-api-php/TextMagicAPI.php'); // Set the username and password information $username = 'myusername'; $password = 'mypassword'; // Create a new instance of TM $router = new TextMagicAPI(array( 'username' => $username, 'password' => $password )); // Send a text message to '999-123-4567' $result = $router->send('Wake up!

' Source: Detect location by IP Here is an useful code snippet to detect the location of a specific IP. Source: Check if server is HTTPS. 10 super useful PHP snippets. Super simple page caching When your project isn’t based on a CMS or framework, it can be a good idea to implement a simple caching system on your pages. The following code snippet is very simple, but works well for small websites. <? 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. » Credit: Wes Bos Calculate distances in PHP Here is a very handy function, which calculate the distance from a point A to a point B, using latitudes and longitudes. 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; } }

10 life-saving PHP snippets. Highlight specific words in a phrase Sometimes, for example, when displaying search results, it is a great idea to highlight specific words. This is exactly what the following function can do: Source: Get your average Feedburner subscribers Recently, Feedburner counts had lots of problems and it’s hard to say that the provided info is still relevant. Source: Automatic password creation Although I personally prefer leaving users to choose their password themselves, a client recently asked me to generate passwords automatically when a new account is created. Source: Compress multiple CSS files If you’re using different CSS files on your site, they might take quite long to load. Header('Content-type: text/css'); ob_start("compress"); function compress($buffer) { /* remove comments */ $buffer = preg_replace('!

Are You Making These 10 PHP Mistakes? Be Careful Using PHP's empty() Published: topics: php Glen Stansberry recently shared his great tutorial about 10 PHP Mistakes, but I thought there was one other mistake so important and easy to make that it was worth explaining: PHP programmers of all skill levels can easily use the empty() language construct incorrectly. 1. Do Use empty() with Arrays PHP's empty() is very useful for dealing with PHP arrays since it returns true if your array variable is null or an empty array, and also gracefully returns true if your array variable is undefined. if (!

2. In many cases, empty() cannot be used with string variables for the simple fact that empty() returns true if your variable is set the the string value '0'. $mystring = '0';if (empty($mystring)) { } $mystring = ' ';if (empty($mystring)) { } However, you cannot simply replace the use of empty() with a conditional that checks if the string variable is equal to null or has a string length of zero.

If (empty($mystring)) { } if ($mystring == null) { } if (! 3. 4. When Pythons Attack. In this article, I will chronicle some of the most common mistakes made by both new and veteran Python programmers, to help you avoid them in your own work. First of all, I should explain that these come straight from first-hand experience. I earn my living as a Python trainer. Over the last seven years, I've had the privilege of teaching over 100 Python classes, to over 1,000 students -- and have watched most of them make the same mistakes. That is, these are things that I've seen real Python beginners do, hundreds of times. "What's that?

" The good news is that once you learn Python, many pitfalls are avoided naturally, thanks to the clean design of the language. But programming Python still isn't quite an automatic task, and forewarned is forearmed. Pragmatic Mistakes Let's start out with the basics; things that people who are just learning how to program tend to get tripped up on, even before they delve into syntax.

Type Python Code at the Interactive Prompt Coding Mistakes.