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.
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.
5 Basic PHP Security Tips
Security should be a top concern throughout the development of any PHP web application. There are some very simple measures you can take to protect your application from potential abuse. This post will cover some of the basics of PHP security. For more detailed explanations of good security practices, check out the PHP Security Guide.
I do not consider myself a PHP security expert, but these are things that every developer should know. Also keep in mind that security is a process and not a result.
Snippet: Maintain a Single Database Object in PHP 5 Using the Singleton Pattern
When creating a PHP application, it is usually necessary to connect to a database to perform certain tasks. In some cases you only want to open a connection when necessary, but limit it to a single connection. This way you don't waste resources on unnecessary database connections. For these situations I use the Singleton Pattern, which is perfect for this.
In this example, we are creating a MySQLi object and forcing it to a single instance. We just need to call DB::get() to create and/or access the object.
