Monthly Archives: April 2012

PHP Tutorial: Faking Domains on Windows

This isn't so much about PHP as it is about setting up your development environment. If you're not a regular Linux user and don't want to be booting in and out of Windows or launching a virtual machine to boot a Linux VM, you're probably going to set up a WAMP (Windows, Apache, MySQL, and…

PHP Tutorial: Timing Your Code

Sometimes you need to know how long a script takes to run, or perhaps just a part of it. Sometimes you can think of two ways to do something and want to know which is the fastest. That's where the microtime() function comes in mighty handy. $start = microtime(true); //code //to //execute $end = microtime(true);…

PHP Tutorial: Is That Value In That Array?

I'm an array junkie. It is my go to data structure. At its simplest, an array is a variable that holds other variables. A basic array indexes the variables with a number. Here's a quick scriptlet that creates and prints out an array: $array = array("fred","barney","wilma","betty"); echo print_r($array,true); That's going to output something like this:…

PHP Tutorial: Validating Email Addresses

When you want to make sure an e-mail address is formatted correctly, a Google search will turn up a number of functions and preg/ereg regular expressions. But one of the easiest ways is to use a PHP function that has been a part of the standard PHP installation since 5.2: filter_var(). It's as simple as:…

PHP Tutorial: Case Insensitive String Comparison

One of my favorite functions in PHP is strtolower. All it does is take the contents in a string and make sure all the big letters are small letters. But it also makes life a bit easier when you're handling user-input content. If you do this comparison: if("James Van Der Beek" == "James van der…

PHP Tutorial: Spotting Even Numbers

I was in a programming interview and asked to solve some coding problems. I don't even remember the problem, but I remember a subtask: determine if a number was odd or even. My first thought was the modulus operator (%). 20 / 6 will return 3.333333. 20 % 6 will return 2. Modulus is like…