PHP is one of the most important languages in web development, known for its flexibility and ability to create web applications quickly. Whether using pure PHP or some of the famous frameworks, such as Symfony and Laravel, the language has been around since the 1990s, playing a significant role on the Internet to this day.

 

As the language evolved, various improvements have been incorporated. Version 8 - released in 2023 - brought new functions, object-orientation and typing facilities, besides having introduced the concept of JIT (Just-In-Time compilation) to the core of the language, increasing execution speed.


As we developers say, “a line of code is better than a thousand words”, so let's take a look at some examples that you can run here.

 

 

Major new features in PHP 8

Simple constructors

It is now possible to create class constructors in a simpler way, eliminating the need to define variables to receive values. See the example below:

class Client { 
    public function __construct(
        private string $name,
        private int $age
        ) {
    }
}

 

str_contains() function

A new str_contains() function makes it easier to find one string inside another:


Before:

$haystack = 'How are you?';
$needle   = 'are';
if (strpos($haystack, $needle) !== false) {
    echo 'true';
}

Now:

$phrase = 'find me here';
echo str_contains($phrase, 'find');


Match expression

The match expression provides a more efficient implementation than the switch, in some cases.

 

Before:

switch (8.0) {
    case '8.0':
        $result = "Oh no!";
        break;
    case 8.0:
        $result = "This is what I expected";
        break;
}
echo $result; // Oh no!

Now:

echo match (8.0) {
    '8.0' => "Oh no!",
    8.0 => "This is what I expected",
}; // This is what I expected

 

These are some of the new features in version 8.0 of the PHP language, which allows frameworks on the market to update and release several features to make life easier for PHP developers, by providing more tools for writing code.

 

 

What's new in version 8.3

I would also like to mention version 8.3, which is the last version released at the time of writing this article. Here are some interesting new features in this version:

 

New function for JSON validation

Before:

function json_validate(string $string): bool
{
    json_decode($string);
    return json_last_error() === JSON_ERROR_NONE;
}
var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

Now:

var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

 

Language override attribute

Before:

use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase
{
    protected $logFile;
    protected function setUp(): void
    {
        $this->logFile = fopen('/tmp/logfile', 'w');
    }
    protected function taerDown(): void
    {
        fclose($this->logFile);
        unlink('/tmp/logfile');
    }
}

Now:

use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase
{
    protected $logFile;
    protected function setUp(): void
    {
        $this->logFile = fopen('/tmp/logfile', 'w');
    }
    #[\Override]
    protected function taerDown(): void
    {
        fclose($this->logFile);
        unlink('/tmp/logfile');
    }
}

 

 

Conclusion

PHP is a programming language that is constantly evolving and is a great choice for developing web applications, from microservices to robust projects.


The language community is active and always striving to improve performance and add new features, which will certainly make it easier to maintain or create projects with PHP.

 

References:

 

 

  Here's a look at one of the projects Alter Solutions worked on with PHP 8.

Share this article