Useful SVN one-liner
Posted on
Add it to your aliases, this one liner adds all unrecognized files in the current directory (preceded by ? in svn status) to a svn project.
svn st | grep '^?' | cut -d ' ' -f 7 | xargs svn add
Posted on
Add it to your aliases, this one liner adds all unrecognized files in the current directory (preceded by ? in svn status) to a svn project.
svn st | grep '^?' | cut -d ' ' -f 7 | xargs svn add
Posted on
The single easiest improvements you can make to the usability of HTML Forms is to correctly use LABELs. And it helps all users, not just a subset. Its also a nice application of Fitt's Law
s Use the label element to make your HTML forms accessible | 456 Berea Street
When checkboxes and radio buttons have properly associated labels, the label text will also be clickable, thus making the target area much larger and easier to hit. This obviously has usability benefits for all users.
Posted on
If you're not familiar with them, anonymous functions seem to be a term of art that very advanced programmers throw around to make others feel dumb. Until now, my closest experience has been in using them with jQuery and javascript, but tonight I found a use for them in PHP. If you don't read the wikipedia article linked before, let me pull out a quote that reflects the "Aha!" moment I had tonight.
Anonymous functions can be used to contain functionality that need not be named and possibly for short-term use.
While true anonymous functions won't be available until PHP 5.3, you can use create_function to generate functions on the fly by passing them a signature and string containing valid php code. These functions are perfect for use with array sorting functions like usort or uasort as well as array_filter.
For this example, suppose we have the following data:
``` {.php style="font-family: monospace;"} $sam = new StdClass;$sam->minutes_played = 1300;$sam->goals = 5;$sam->fouls = 10; $max = new StdClass;$max->minutes_played = 1900;$max->goals = 6;$max->fouls = 10; $joe = new StdClass;$joe->minutes_played = 1100;$joe->goals = 4;$joe->fouls = 8; $team = array($sam, $max, $joe);
You've been tasked with reporting on the data, specifically, finding
who has the most minutes, goals, and fouls. Traditionally, you'd be
inclined to write three functions that sort the array by field you are
interested, and return the elements with the highest values. It's a bit
of a brute force approach, but let's see how we can do this with a
single function. Below is that function.
``` {.php style="font-family: monospace;"}
/** * Returns an array of all elements in the array that have the highest value in a given field. * @param string field * @param array stats data * @return array */public function findMaxStat($field, $data){// sorting comparision$sort_code = ' if ($a->' . $field . ' == $b->' . $field . ') return 0; return ($a->' . $field . ' < $b->' . $field . ' ? -1 : 1);'; // get the data sorted highest to lowest by the field we desire.usort($data, create_function('$a,$b', $sort_code));$data = array_reverse($data); // remove all elements of the array that do not match the max value$max = $data[0]->$field;$filter_code = 'return ($a->' . $field . ' == ' . $max . ');';$values = array_filter($data, create_function('$a', $filter_code)); return $values;}
What's going on here?
What's the big deal here? Well, with one function we quickly get the rows we care about.
{.php style="font-family: monospace;"}
// returns $sam and $joe$max_fouls = findMaxStat('fouls', $team);// returns $max$max_minutes = findMaxStat('minutes_played', $team);// returns $max$max_goals = findMaxStat('goals', $team);
More importantly, if we add more fields to our data, we don't have to write more code!
Used correctly, anonymous functions provide very customized functionality that can save you from having to write more code. Also, by learning to use PHP's built-in array manipulation functions, you'll quickly learn how to slice and dice nested arrays and arrays of objects without resorting to complicated loops and if statements. I'm not sure what the performance implications might be of using the technique, but if it saves you from writing long, complicated code-blocks it's worth the trade off. There's a lot of existing discussion about create_function, and its impact on memory usage and DANGER!