The Thing About PHP 7’s Spaceship Operator

Subscribe So You Never Miss An Episode

[saf]

May the force be with you… 😉

Unless you’ve been under a rock (which is totally cool if so) you know PHP 7 has been out for a bit now. I finally took some time the other day to fire it up on one of my dev servers and see what all the fuss is about.

Online, I’d kept hearing about this “spaceship” operator.

This thing’s kind of cool actually. Let me show you.

So, here’s a common bit of code you might see pre-PHP7:

rp1

This is a sorting routine.

usort() lets you sort an array using a user-defined comparison function… in this case compare_length(). So, what this does is re-arrange the $names array to be in order of the length of the names in it. Here’s the output:

rp2

Ok, simple enough.

But, notice the big compare_length() function. It works fine, but that’s a good chunk of code just to run a simple comparison. And, this is where the spaceship operator comes in. Here’s what the function looks like with PHP 7:

rp3

Just three lines now. Pretty sweet!

So, how does the spaceship operator (<=>) work?

You can use it just like you might any other operator. For example, equals:

1 == 1;

Except it looks like this in spaceship mode:

1 <=> 1;

When you do that, PHP will run a “trinary” comparison. So, it’ll check to see if 1 is less than, equal to or greater than 1. Thus the syntax of a less than sign (<), an equal sign (=) and a greater than sign (>): <=>.

And here’s what it’ll return…

If $a is less than $b, it returns -1. If $a is equal to $b, it returns 0. And, if $a is greater than $b, it returns 1. Here’s an example:

rp4

Slicky ricky, eh?

Anyhoo, thought I’d give you the low down on it. You’re welcome! 😉

If you want to learn even more PHP so you can still cram your skull with Cheetos and binge watch Star Wars movies without having to deal with that annoying boss, then get your butt over to: http://johnmorrisonline.com/learnphp and take my free PHP course.

P.S. If you liked the show, give it a like and share with the communities and people you think will benefit. And, you can always find all my tutorials, source code and exclusive courses on Patreon.

Do you want more freelance clients?

I’ll show you what I learned over the last 15 years to grind out (from absolute scratch) a backlog of new clients wanting to hire you. Who your best client prospect are, what services you should be offering them, where to find them and more. Just enter your email address in the box below and let’s get started:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

You might also like

Do you want more freelance clients?

Enter your email below to get started building your system for consistently bringing in new freelance clients:

WHAT OTHERS ARE SAYING

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Sukh Plaha

John is a fantastic and patient tutor, who is not just able to share knowledge and communicate it very effectively – but able to support one in applying it. However, I believe that John has a very rare ability to go further than just imparting knowledge and showing one how to apply it. He is able to innately provoke one’s curiosity when explaining and demonstrating concepts, to the extent that one can explore and unravel their own learning journey. Thanks very much John!

Ray Edwards

I recommend John every chance I get. If every person I worked with were as committed to excellence, punctuality, value, and unquestionable integrity… the world would be a better place. Highest recommendation.

Close Menu