Categories
Allgemein

10 lesser known PHP tricks

PHP is one of the most dynamic and powerful scripting languages. Many developers are familiar with its basic features, but there are many lesser known PHP tricks that can improve your coding skills. Not only will these tricks make your development process easier, but they will also add elegance to your code.

In this article, we’ll explore some of the lesser known PHP tricks. We’ll explore techniques that may just be the missing piece in your PHP toolbox. So, what are you waiting for? Let’s dive in!

10 lesser known PHP tricks

PHP is one of the most dynamic and powerful scripting languages. Many developers are familiar with its basic features, but there are many lesser known PHP tricks that can improve your coding skills. Not only will these tricks make your development process easier, but they will also add elegance to your code.

In this article, we’ll explore some of the lesser known PHP tricks. We’ll explore techniques that may just be the missing piece in your PHP toolbox. So, what are you waiting for? Let’s dive in!

01. Null coalescing operator ??

The Null coalescing operator returns its first operand if it exists and is not NULL, otherwise it returns its second operand which can be a default value.


    $result = $variable ?? 'default';

02. Array destructing

Easily extract values from arrays and assign them to variables.


    $person = ['John', 'Doe'];
    [$firstName, $lastName] = $person;

03. Ternary operator with multiple conditions

A compact way to handle multiple conditions.


    $result = ($condition1 && $condition2) ? 'true' : 'false';

04. Using list() with each()

Iterating over an associative array and extracting key-value pairs.


    $person = ['name' => 'John', 'age' => 30];
    while (list($key, $value) = each($person)) {
        // Process key and value
    }

05. Anonymous classes

Create one-off objects without explicitly defining a class.


    $obj = new class {
        public function doSomething() {
            // Your code here
        }
    };
    $obj->doSomething();

06. The yield keyword for generators

Efficiently iterate over large datasets without loading everything into memory.


    function largeDataSetGenerator() {
        for ($i = 0; $i < 1000000; $i++) {
            yield $i;
        }
    }

07. Variable functions

Use a variable to call a function dynamically.


    $operation = 'strtolower';
    $result = $operation('Hello World');

08. array_column() for extracting values from multidimensional arrays

Quickly extract values from arrays of arrays.


    $users = [
        ['id' => 1, 'name' => 'John'],
        ['id' => 2, 'name' => 'Jane'],
    ];
    $names = array_column($users, 'name');

09. array_replace_recursive() for deep array merging

Merge arrays with nested structures.


    $array1 = ['fruit' => ['apple' => 1, 'banana' => 2]];
    $array2 = ['fruit' => ['banana' => 3, 'cherry' => 4]];
    $result = array_replace_recursive($array1, $array2);

10. get_defined_vars() for debugging

Get an array of all defined variables in the current scope.


    function debug() {
        $vars = get_defined_vars();
        var_dump($vars);
    }