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.
![Write Better JavaScript with Promises]()
You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...
![CSS Gradients]()
With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements. CSS gradients are another step in that direction. Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...
![RealTime Stock Quotes with MooTools Request.Stocks and YQL]()
It goes without saying but MooTools' inheritance pattern allows for creation of small, simple classes that possess immense power. One example of that power is a class that inherits from Request, Request.JSON, and Request.JSONP: Request.Stocks. Created by Enrique Erne, this great MooTools class acts as...
![Create WordPress Page Templates with Custom Queries]()
One of my main goals with the redesign was to make it easier for visitors to find the information that was most popular on my site. Not to my surprise, posts about MooTools, jQuery, and CSS were at the top of the list. What...
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!