PHP Splat Function

By  on  

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.

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

  • By
    Smooth Scrolling with MooTools Fx.SmoothScroll

    I get quite a few support requests for my previous MooTools SmoothScroll article and the issue usually boils down to the fact that SmoothScroll has become Fx.SmoothScroll. Here's a simple usage of Fx.SmoothScroll. The HTML The only HTML requirement for Fx.SmoothScroll is that all named...

  • By
    iPhone-Style Passwords Using MooTools PassShark

    Every once in a while I come across a plugin that blows me out of the water and the most recent culprit is PassShark: a MooTools plugin that duplicates the iPhone's method of showing/hiding the last character in a password field. This gem of...

Discussion

  1. Slick!

  2. splat()’s a pretty cool sounding function name, but I still prefer PHP’s explode().

  3. @Will: And the both deal with arrays!

  4. 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

  5. @Seth: Do a site search for “ternary.” That will get you all of my articles on the topic.

  6. You can also use typecasting:

    $a = (array) $b; instead of $a = splat($b);

  7. 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;
    }

  8. Been working with forms a lot lately while I convert some of my flat php stuff to Ajax / Mootools. This is fantastic!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!