10 Common PHP Mistakes to Avoid
These are some very common mistakes that are made in PHP. Some of these can be tricky to catch and can lead to all sorts of strange behavior. So here are 10 common PHP coding mistakes to avoid.
1 '=' Vs. '=='
Using a single '=' in a comparison will cause an assignment and return true, so this mistake can have some pretty unexpected results. It can be hard to catch since it looks perfectly valid to the interpreter if you are comparing something with a variable.
An easy way to avoid this is to swap the subject and variable like this:
< ?php if(true = $something) { // Parse error! // do stuff } ?>
The above will result in a parse error since you can't assign a literal to something, making it easy to catch and fix.
2 '==' Vs. '==='
There is a big difference between the '==' (equal) and '===' (identical) comparison operators. '==' will convert types to match before making the comparison, while '===' will compare directly without converting. So in situations where the difference between '0' and 'false' matters, you must use '==='. Here's some examples:
< ?php var_dump(false == 0); // true var_dump(false === 0); // false var_dump(false === false); // true var_dump('0' == 0); // true var_dump('0' === 0); // false ?>
PHP: Process Array Items With array_map
Let's say you want to run a function on each item in an array. For example, you want to run strip_tags() on all $_POST data. One way to accomplish that is to use a foreach loop and reassign each array element manually, but there's a function for that. The array_map function accepts the name of a function and an array or arrays to run the function on.
So to accomplish our simple example, this is all it takes:
$original = array('<p>Paragraph</p>', '<strong>Bold</strong>'); $new = array_map('strip_tags', $original); // $new is now array('Paragraph', 'Bold');
You can supply any function, including any you define for more advanced use:
$original = array('<p>Paragraph</p>', '<strong>Bold</strong>'); $new = array_map('clean_input', $original); function clean_input($value) { return strip_tags($value, '<p>'); // allow p tags } // $new is now array('<p>Paragraph</p>', 'Bold');
The array_map function is a powerful utility when it comes to working with arrays. You can do things in one line that would otherwise require loops and other complex structures. The examples here are just very basic, but are handy for many everyday tasks. Check the examples in the PHP documentation for other tricks array_map can do.
The ‘War’ on IE6
Recently I've been noticing a trend among Web developers to "take action" against MS Internet Explorer 6. From putting badges on their Twitter avatars to designing websites to detect and block IE6, many designers and developers are coming up with their own ideas to help end the browser's reign of terror. My question is, are some of these methods appropriate or even in their best interests?
Default Form Values with jQuery
Here's a useful jQuery snippet that clears the default values of form fields on focus and refills them if no text is entered. It uses the attribute called defaultValue which stores the original value of a form field.
$(document).ready(function() { $('input[type=text]').focus(function() { if($(this).val() == $(this).attr('defaultValue')) { $(this).val(''); } }) .blur(function() { if($(this).val().length == 0) { $(this).val($(this).attr('defaultValue')); } }); });
For example, assume you have a field like this:
<input type="text" value="Search..." />When the page loads, the text field will have "Search..." filled in. When you focus on it, this text will disappear (assuming it hasn't already been edited by you). When you leave focus without typing anything, the text will reappear.
PHP File Downloads
PHP can be used to securely control access to file downloads. This tutorial will show how you can send file through a PHP script and limit the download rate. The function we will write accepts the path to the file to send and optionally a rate in kB/s to limit the transfer speed. The function should also be able to handle range headers from clients that allow stopping and resuming downloads.
Basic Overview of Loops in PHP
PHP comes with several control structures for creating loops. A loop is basically code that executes repeatedly for as long as it needs to. The control structures I'll discuss here are: while, do-while, for, foreach, break & continue. Each of these are similar, and some can pretty much be interchangeable, but some are better suited for certain tasks than others.
