New Features in PHP 8.0

String to float PHP

The following are some of the new key features of PHP 8

JIT (Just in time)  

JIT is the most prominent feature of PHP 8. PHP JIT is an independent part of OPcache. It can enable and disable at runtime and compile time.

Consider that JIT stands for Just In Time compiler. It is a way of executing computer code during the execution of the program rather than before execution.

Therefore, JIT translates PHP byte code to machine code. This function has improved the performance of applications which has heavy mathematical functions. It increases the performance of PHP applications, as during runtime it can compile generated code into the native machine code. If the JIT is enabled the code will be run by the CPU itself, that’s why it makes PHP very fast.
According to RFC, the potential to move more code from C to PHP has increased because PHP is sufficiently fast. 

Union types  

Union types are a very important function in PHP 8 because PHP is a dynamically typed structure. Currently, PHP supports two types of union types, some type of null and array or traversable. In PHP 8, union types accept values of multiple types, rather than a single one which indicates either one of those can be used. 

Attributes 

The attribute function is available in many other languages like  C#, C++, Rust, and others. Before PHP 8, PHP supported an unstructured form of metadata. Now in the new version, you can use structured metadata with PHP native syntax. Attributes offer the ability to add machine-readable metadata information that can be used to specify properties for objects, elements, or files.  

Error Handling 

Before this latest up-gradation PHP emits a warning and returns null when encounters a  value that it cannot use. As PHP warning does not halt the remaining block so this behavior was not desirable. Now in PHP 8 internal functions can throw an exception for type errors or value errors. Passing an illegal parameter to a user-defined function is a type error. Now instead of a warning, PHP can throw exceptions.  

WeakMaps 

To improve performance and prevent memory leaks in long-running processes, PHP 8 introduced weak maps. Weakmap is a collection of data objects in which keys are weakly referenced.  A weak map is a cache of data derived from an object that does not need to live longer than an object. If the object falls out of scope, it will not prevent the garbage collector from clearing the object. 

Nullsafe operator

Nullsafe is basically short-circuiting means skipping the evaluation of an expression based on some given condition. PHP Nullsafe operator is a new feature that provides optional chaining to PHP. It short circuits the retrieval if the value is null, without causing any errors. The null safe operator is ?->

Match Expression 

Match Expression is similar to switch statement l, it has a subject expression that is compared against multiple alternatives. It supports single-line expressions and doesn’t need a break statement. Match Expression does strict comparison. 

For example in PHP 7 and older versions: 

Switch ( 7.0 ) { 
  Case '7.0’ :
           $answer  = “Beautiful”
        Break;
Case 7.0 : 
    $answer = “wonderful” 
Break; 
} 
Echo $answer

In PHP 8 we can write a Match Expression:

Echo match (8.0) { 
      ‘8.0’ => “Beautiful” 
      8.0 => “Wonderful” 

};
String to float PHP
en English
X
Scroll to Top