November 26th, 2021

PHP
Modern PHP Cheatsheet

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.

Statamic Ninja

Comments

Marian Pop

PHP / Laravel Developer. Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.