HP has come a incredibly long way. If you are still writing PHP the same way you did five or ten years ago, you are missing out on some powerful features that can make your code cleaner, faster, and more robust.
With the massive improvements introduced in PHP 8 and beyond, the language is more elegant than ever. Here are seven essential PHP tips and tricks you should start using today to instantly level up your development game.
Before PHP 8.0, defining a simple Data Transfer Object (DTO) or class required a lot of repetitive typing: declaring properties, adding constructor parameters, and assigning them. Constructor property promotion does all three in one go.
The Old Way:
<?php
class User {
public string $name;
public string $email;
public function __construct(string $name, string $email) {
this->name = $name;
this->email = $email;
}
}The Modern Way:
<?php
class User {
public function __construct(
public string $name,
public string $email,
) {}
}Why it's great: It significantly reduces code clutter, making your classes easier to read and maintain.
The traditional switch statement has a lot of flaws: it requires break statements to prevent fall-through, does loose type checking, and doesn't return a value. The match expression fixes all of this.
The Modern Way:
<?php
$status = 200;
$message = match ($status) {
200, 201 => 'Success!',
400, 404 => 'Not Found',
500 => 'Server Error',
default => 'Unknown Status',
};Why it's great: match is strictly typed, implicitly breaks, and returns a value directly, making conditional assignments safe and concise.
We’ve all written code to check if an object exists before calling a method on it to avoid the dreaded "Call to a member function on null" error. The nullsafe operator (?->) short-circuits the execution if the object is null.
The Old Way:
<?php
$country = null;
if ($user !== null && $user->getAddress() !== null) {
$country = $user->getAddress()->getCountry();
}The Modern Way:
<?php
$country = $user?->getAddress()?->getCountry();Why it's great: It flattens your logic and gets rid of deeply nested, defensive if blocks.
You no longer have to pass variables in the exact order a function expects them. By using named arguments, you can pass data based on the parameter name. This is incredibly helpful for functions with many optional parameters.
The Modern Way:
<?php
// Standard function
setcookie(
name: 'test',
expires_or_options: time() + 3600,
secure: true,
httponly: true,
);Why it's great: You can skip optional arguments without passing null multiple times, and your code becomes instantly readable to anyone reviewing it.
If you are passing simple callbacks to array functions like array_map or array_filter, traditional closures can feel overly verbose. Arrow functions (fn() =>) offer a cleaner syntax and automatically capture variables from the parent scope.
The Old Way:
<?php
$multiplier = 2;
$numbers = [1, 2, 3];
$doubled = array_map(function($number) use ($multiplier) {
return $number * $multiplier;
}, $numbers);The Modern Way:
<?php
$multiplier = 2;
$numbers = [1, 2, 3];
$doubled = array_map(fn($number) => $number * $multiplier, $numbers);Why it's great: It strips away the function, use, return, and curly braces, keeping your array manipulations crisp.
PHP is loosely typed by default, meaning it will silently convert a string "5" to an integer 5 if a function requires it. This can lead to insidious, hard-to-track bugs.
The Fix: Always put this as the very first line of your PHP files (after the <?php tag):
<?php
declare(strict_types=1);Why it's great: It forces you and your team to pass the exact data types specified. If a function expects an int and you pass a string, PHP throws a TypeError. Fail fast, fix fast!
Array destructuring allows you to extract data from an array and assign it to variables in a single line. It's the modern equivalent of the old list() function.
The Modern Way:
<?php
$user = ['John', 'Doe', 'johndoe@example.com'];
// Extract variables directly
[$firstName, $lastName, $email] = $user;
echo $firstName; // Outputs: JohnYou can even skip elements you don't need by leaving a blank space:
<?php
[ , , $email] = $user; PHP is no longer the chaotic scripting language it was in the early 2000s. It has matured into a highly performant, strongly-typed, and elegant language. By incorporating these modern tricks into your daily workflow, you will write code that is not only faster but significantly easier to maintain and read.
What is your favorite modern PHP feature? Let me know in the comments below!
Technical writer and developer at DigitalCodeLabs with expertise in web development and server management.
Get the latest posts delivered straight to your inbox.