November 26th, 2021

68747470733a2f2f692e696d6775722e636f6d2f325354457467472e706e67.png

Modern PHP Cheatsheet

PHP is far from dead. We can see the language itself maturing and the amazing tools and frameworks at the same time. There are lots of great articles and videos comparing and showcasing the new features added in PHP 8 but, recently I cam across a github repo doing just that, showcasing the new features and their implementation.

Let take a look at this example of function default parameter value:

You can set default value to your function parameters:

1function myFunction($param = 'foo')
2{
3 return $param;
4}
5$a = myFunction();
6// $a = 'foo'
7 
8$b = myFunction('bar');
9// $b = 'bar'

But if you send null or an undefined property, default value won't be used:

1function myFunction($param = 'foo')
2{
3 return $param;
4}
5$a = myFunction(null);
6// $a = null
7 
8$b = myFunction($undefined); // PHP Warning: Undefined variable $undefined
9// $b = null

For a complete list of features and implementations, visit the github repo.

Follow @LaravelMagazine on Twitter →
Marian Pop

I’m a Full-Stack Developer based in Transylvania. I use Laravel daily whether it’s for work or my personal projects.

Newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox every Friday!

We'll never share your email address and you can opt out at any time.