UltraMega Blog
3Nov/090

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.

15Oct/091

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.

29Sep/092

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.

21Sep/092

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.

1Sep/093

Degradable Tabs With jQuery UI

Creating tabbed content is easy with jQuery UI. Using a simple HTML layout and calling the tabs function is all it takes. Here, I'll show you how to make a degradable tabbed interface. That is, we'll make it so the page is still readable when JavaScript is disabled. This involves hiding and showing elements using JavaScript before enabling tabs.

See the live demo. Try turning off JavaScript to see how it degrades.

26Aug/094

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.