Top 10 Hidden Features of PHP, Which I didn’t Knew | 2022

What kind of internet could it be if all the websites were just information and links? This isn’t easy to imagine these days when most web pages are interactive and engaging. These web pages are called dynamic web pages

Dynamic web pages are web pages that display different content every time they are viewed while maintaining the same layout and design.

PHP is one back-end language that can be used as a scripting language. A server-side dynamic website page is one whose content is controlled and managed by an application server that processes server-side scripts at the back end.

Features of PHP
PHP

PHP is a server-side scripting language that can easily be embedded in HTML code. A scripting language, a programming language used in runtime environments, automates the execution and performs other tasks like creating extensions and plugins or system administration. 

It is a programming language that runs on web servers. The applications do not require web browsers. PHP stands out from other scripting languages because of its unique features. This article will discuss the features of PHP and why PHP is so beloved by its users.

1. Enumerations

PHP 8.1 now supports Enumerations (also known as the enum). This is an itemized type, which can hold a fixed number of possible values.

<?php
enum Suit
{
    case Hearts;
    case Diamonds;
    case Clubs;
    case Spades;
}

function do_stuff(Suit $s)
{
    // ...
}

do_stuff(Suit::Spades);
?>

2. Fibers

PHP 8.1 now supports Fibers, a low-level component that allows concurrent code execution. A code block called Fiber contains its stacks of variables and states. These Fibers are considered application threads and can therefore be started from the main program. 

$fiber = new Fiber(function() {
    Fiber::suspend(42);
});
$return = $fiber->start();
echo $return;

The main program can’t suspend or terminate the Fiber once it is started. The Fiber code block cannot be used to suspend or terminate it. The control of the Fiber is then returned to the main program. It can execute the Fiber at the same point that it was suspended.

Fiber does not permit simultaneous execution of multiple Fibers, the main thread, or a Fiber. It is an advantage for PHP frameworks to manage the execution stack efficiently and allow for asynchronous execution.

Detailed Article on Fiber

3. The ‘never again’ Return Type

PHP 8.1 now supports a new type of return called never. You can use the never type to indicate that a function will end its execution after completing tasks. This can be accomplished by either throwing an exception or calling the exit() and die() functions.

function redirect(string $uri): never 
{
    header('Location: ' . $uri);
    exit();
}

The never return type is very similar to the empty return type. However, the void return type continues execution after the function has completed a set of tasks.

4. The Readonly Property

PHP 8.1 now supports a new class property, read-only. A class property marked as read-only cannot be initialized more than once. You cannot change the value inside.  The application will throw an exception if you attempt to change the value forcibly.

class Example {
    private $__readOnly = 'hello world';
    function __get($name) {
        if($name === 'readOnly')
            return $this->__readOnly;
        user_error("Invalid property: " . __CLASS__ . "->$name");
    }
    function __set($name, $value) {
        user_error("Can't set property: " . __CLASS__ . "->$name");
    }
}

5. Final Class Constants

PHP 8.1 now supports final, a flag for class constants. These final class constants cannot be changed, even through inheritance. Subclasses can’t extend or override them.

class Foo {
  public const ABC = 'Something';
}

class Bar extends Foo {
  public const ABC = 'Nothing'; // php allows to override this const
}

// but if we use final constants
class Foo {
  final public const ABC = 'Something';
}

class Bar extends Foo {
  public const ABC = 'Nothing'; // not allowed
}

// Fatal error: Bar::ABC cannot override final constant Foo::ABC

This flag cannot be used to declare a private constant because it is not accessible outside of the class. If you declare both private and final constants, a fatal error will be made.

6. New Function ‘array_is_list()

PHP 8.1 now supports array_is_list(), a new function that allows you to identify if an array contains all sequential integers starting from 0. If the array is a semantic listing of values, it returns true. 

This function returns true in an array whose keys begin at 0 and all integers between them. It returns true even for empty arrays. 

array_is_list([]); // true
array_is_list([1, 2, 3]); // true
array_is_list(['apple', 2, 3]); // true
array_is_list(['apple', 'orange']); // true
array_is_list([0 => 'apple', 'orange']); // true
array_is_list([0 => 'apple', 1 => 'orange']); // true

The following code snippet will show you how to use array_is_list(). A list whose keys do not start at zero or are not integers, or whose key sequence is not sequential will be deemed false.

7. New Functions ‘fsync() & ‘fdatasync()

PHP 8.1 now supports the fsync(), fdatasync() function. They are similar to the fflush() function used to flush buffers into an operating system. The fsync() function and fdatasync() can flush the buffer into the storage. 

The only difference is that the fsync() functions include metadata when synchronizing file changes. The fdatasync() function does not.

The function fsync() will use a file pointer to attempt to make changes to the disk. If the resource is not a folder, it will return true on success and false on failure. 

The fdatasync() function works the same, but it is faster. fsync() will try to sync the file’s metadata and data changes. This is technically two disk writes.

8. Support for String-Keyed String-Keyed Arrays: Array Unpacking

PHP 8.1 now supports unpacking string-keyed arrays. PHP uses the spread (…) operator to unpack an array. However, it has a simpler syntax.

The spread operator was only compatible with arrays with numeric keys before PHP 8.1.

9. Directory Uploads: New ‘full_path Key’ in ‘$_FILES

PHP 8.1 supports a full_path key for the $_FILES global variable. PHP 8.1 was the first PHP version to support full_path keys in the $_FILES global variable. Before PHP 8.1, $_FILES did not store relative paths or exact directories to the server. 

You couldn’t upload an entire directory via an HTML file upload form. The new full_path key solves this problem. It stores the relative paths, reconstructs the exact directory structure on a server, and allows directory uploads. 

10. New ‘IntlDatePatternGenerator’ Class

PHP 8.1 adds support for a new IntlDatePatternGenerator class. It was only possible to create a localized time and date using the IntlDateFormatter before PHP 8.1. 

It supports eight predefined formats, which can be used to create a localized date and time. But these formats are not as customizable as those provided by IntlDatePatternGenerator. 

This class allows you to specify the format for a date and month. The class will automatically take care of the order. 

Conclusion

The language you choose will depend on the feature you want and what you’re trying to build. In the World Wide Web age, scripting languages are highly in demand to create dynamic web pages. PHP has many outstanding features. 

There are many other outstanding features that we haven’t covered. It is popular among other scripting languages because it is open-source, reliable, and fast. Its popularity is evident by its status as one of the most popular programming languages, alongside languages like Java, Python, and C+.

Scroll to Top