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 high-paying freelance clients?

I’ll show you what I learned over the last 15 years about building a 100K freelance business from complete scratch. What I did to land projects with Inc. Magazine, Tim Ferris, Lewis Howes, Michael Hyatt and others. Just enter your email address in the box below and let’s get started:

Leave a Reply on Twitter

Due to excessive spam, I’ve disabled comments on my website. If you have a question or would like to leave a comment, don’t hesitate to contact me on Twitter.

You might also like

Hack Culture Needs to Die

Anybody else tired of “hack” culture? “7 Hacks to Triple Your…” “A Secret Hack to Instantly Double Your…” “52 Little-Known Hacks to…” I asked my

Read More »
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.

The simple secret to high-paying freelance clients?

Enter your email below and I’ll show how to charge more and build a 100K freelance business from scratch:

Clients Like:

Inc. Magazine Logo
Lewis Howes Logo
Ray Edwards Logo

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.

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.

Jim DeJonge

Jim DeJonge

John has a relaxed and engaging manner. His advice is solid and the explanations are well thought out.

Jason Rumley

Jason Rumley

John has a habit of over-delivering on the expectations he sets up. That’s why he’s the best.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

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.

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.

Andrew Malone

Andrew Malone

John Morris is exceptional in his ability to give focused insight into Freelancing and starting one’s business. His direct methods inspire confidence in his honesty.

Michael Skye

Michael Skye

John is a man of integrity, who gives generously of himself to projects and people he cares about.

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

Aaron Gott

Aaron Gott

John has a particular knack for the development and training of others.

Steve Dimmick

Steve Dimmick

John has provided expert knowledge and advice on multiple occasions that have helped me better serve my clients. John is a Rockstar!

Daniel Mohlendick

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