The BEST parts of PHP7

preview_player
Показать описание
In this video we'll be learning about some of the coolest new features of PHP7, including static typing (sort of) and the spaceship operator.

Don't forget to subscribe for more!
Рекомендации по теме
Комментарии
Автор

CORRECTION: At 3:18 I said "double equals", I should have said "double question mark". My bad! 🤭

howCode
Автор

I LOVE the spaceship operator!

In all other languages, and before 7, I had to do so many long methods to determine which value was bigger. It took at least 2 if statements, 3 if it was important to know if they were equally big, and then the return in it.

Now I can skip making the function all together and just ask straight where I would've called the function. With PHP's fancy questionmark operator, I don't even have to type out an if statement for it. It's all condensed down to:

return val1 <=> val2 ? val1 : equal : val2;

morphman
Автор

The double questionmark feature might benefit from another explanation:

It will set the value of the variable to be the first valid input. In the example in the video, $a was Null and therefore not a valid input.

You can do the same with strict typing now, so if you're asking for an int strictly and you put in $var = $a ?? -1, then $a must be an int (and not a string, array or anything else, though a float will work, but it will get truncated), otherwise $var will become -1.

morphman