PHP Splat Function
One of my favorite MooTools functions is $splat(). Splat takes a given argument and returns an array if the argument is not already an array. This is especially helpful when I've coded my script to cycle through an array of items for processing one at a time.
The PHP Code
function splat($input)
{
return is_array($input) ? $input : array($input);
}
When do I use this? Lets say I have a list of items that I can delete individually or alternatively I can use checkboxes to select many to delete at once. Using splat() will ensure my code works if the user chooses to delete one item individually.
![Page Visibility API]()
One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?
![39 Shirts – Leaving Mozilla]()
In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...
![Flext: MooTools Auto-Growing Textrea Plugin]()
A while back David Walsh published a list of
7 MooTools Plugins You Should Use on Every Website
which included 'AutoGrow' a text area expander plugin. 'AutoGrow' is very similar in results to the class I wrote for Education.com, Flext. I decided to release this...
![Sexy Opacity Animation with MooTools or jQuery]()
A big part of the sexiness that is Apple software is Apple's use of opacity. Like seemingly every other Apple user interface technique, it needs to be ported to the web (</fanboy>). I've put together an example of a sexy opacity animation technique...
Slick!
splat()’s a pretty cool sounding function name, but I still prefer PHP’s explode().
@Will: And the both deal with arrays!
David,
I see you writing (if) statements in a unique way that looks a lot cleaner…me being OCD I am really into this idea…can you explain more in depth the basics of writing an if statement this way…preferably an example of an if, else, and else if…that would be awesome.
Thanks,
Seth
@Seth: Do a site search for “ternary.” That will get you all of my articles on the topic.
You can also use typecasting:
$a = (array) $b;
instead of$a = splat($b);
I agree with Bob and usually use typecasting for this sort of things.
There are small differences, however. For example, if you pass an object into splat, you’ll get an object inside of the array. If you typecast an object, you will get an array containing the objects properties. That is one point to typecasting, in my opinion.
Splat, on the other hand, handles null better. If you pass null into splat, you will get a null inside of the array. If you typecast a null, you will get an empty array. This is disappointing because this means there is no way to differentiate between $input being empty and $input being null.
Keep in mind that if you typecast an array, you get the same array, not the same array inside another array.
Therefore, you can get the best of both worlds with the following:
function splat($input)
{
return is_null($input) ? array(null) : (array) $input;
}
Been working with forms a lot lately while I convert some of my flat php stuff to Ajax / Mootools. This is fantastic!