Found 1046 Articles for PHP

What is Trailing Comma in PHP?

Urmila Samariya
Updated on 01-Apr-2021 06:41:46

1K+ Views

Trailing commas are being used in PHP since PHP 7.2 version. We can use trailing commas in the last item in the array. We can add the element of the array without modifying the last line of the item if the line is already using a trailing comma.Trailing Commas before PHP 8.0Before PHP 8, we were not able add a comma to the end of the last parameter.Examplefunction($x, $y, $z){ }In PHP 8.0In PHP 8, we can add trailing commas at the end of the last parameter. PHP 8 allows using the trailing commas in the parameter list and Closure ... Read More

What is Stringable Interface in PHP 8?

Urmila Samariya
Updated on 01-Apr-2021 07:02:34

327 Views

In PHP 8, a new Stringable Interface (__toSting) is added. This method starts with the double underscore (__). The __toString method allows getting an object represented as a string. When a class defines a method using __toString, then it will call an object whenever it needs to treat as a string.Example: Stringable Interface using __toString Live DemoOutputEmployee NameIn PHP 8, the Stringable interface makes it easy to pass strings. A Stringable interface adds automatically once a class implements the __toString method. It does not require implementing the interface explicitly. The Stringable interface can be helpful for type hinting whenever strict types ... Read More

Nullsafe Operator in PHP 8

Urmila Samariya
Updated on 01-Apr-2021 06:33:16

836 Views

PHP 8 uses nullsafe operator instead of a null check condition. Using the nullsafe operator, we can use a chain of calls. While evaluating the elements, if one chain element fails, then the execution of the entire chain will abort and it evaluates to null.When the left-hand side operator evaluates to null, then the whole chain of execution will stop and it evaluates to null. If it does not evaluate to null, then it will behave like a normal operator.The nullsafe operator can be chained, and the expression will be short-circuited from the first nullsafe operator that meets null.$employee->getDepartment()?->getAddress()->format();The nullsafe ... Read More

Match Expression in PHP 8

Urmila Samariya
Updated on 01-Apr-2021 06:32:54

590 Views

Match expression is a new feature that is added in PHP 8. It is very much similar to switch-case statements, but it provides more safe semantics.Match expression does not use the 'case and break' structure of switch-case statements. It supports joint conditions, and it returns a value rather than entering a new code block.We can store match results in a variable because it is an expression.Match expression does not need a break statement like a switch. It supports only single-line expression.Example: PHP 7 Using Switch Statement Live DemoOutputHello World!Example: Above PHP 7 Code Using PHP 8 Match ExpressionOutputLooks Good!Example: Using PHP ... Read More

Constructor Property Promotion in PHP 8

Urmila Samariya
Updated on 01-Apr-2021 07:03:27

664 Views

In PHP 8, Constructor Property Promotion is added. It helps to reduce a lot of boilerplate code while constructing simple objects. This feature allows us to combine class fields, constructor definition, and variable assignments, all in one syntax, into the constructor parameter list.We can say that instead of specifying class properties and a constructor, we can combine all of them using constructor property promotion.Example 1: PHP 7 CodeExample 2: PHP 8 codeWe can re-write the above PHP 7 code in PHP 8 as follows −Output10.9 20 30.8In the above code, we combined property definition and population inline in the constructor ... Read More

Mixed Pseudo Type in PHP 8

Urmila Samariya
Updated on 01-Apr-2021 07:03:58

617 Views

The mixed type in PHP 8 is a new built-in union type. Mixed type is equivalent to array|bool|callable|int|float. Mixing the type is not quite similar to omitting the type completely.That means, the programmer just forgot to write it.Sometimes the programmer prefers to omit some specific type to keep the compatibility with an older version.Mixed type in PHP 8 can take any type of property/return/parameter. We can say that it includes null, callable, resource, all class objects, or all scalar types in PHP. The mixed type is equivalent to the Union type.int|float|bool|string|null|array|object|callable|resourceExample: Mixed Type in PHP 8In PHP 8, Mixed is ... Read More

Union Type in PHP 8

Urmila Samariya
Updated on 01-Apr-2021 06:26:20

654 Views

Using Union Type in PHP 8, we can use the values of two or more types, instead of using a single type. To specify multiple types, vertical line (|) is used to join them.Union type supports parameters, return types, and properties.Syntaxtype1|type2|……|type_nExample 1: Union TypeExample 2: PHP 8 Program using Union TypeOutput511.54Nullable Types in Union TypeIn PHP 7.1, nullable type is used with the question mark ?type. In PHP 8, we can declare nullable types as type|null. For example: float|int|null, but we cannot declare it as ?float|int.Nullable Types Syntaxtype1|type2|nullWe should not declare like ?type1|type2 because this would be an ambiguous declaration.Compile-time ... Read More

Attributes in PHP 8

Urmila Samariya
Updated on 01-Apr-2021 06:24:08

3K+ Views

Attributes are kinds of classes that can be used to add metadata to other classes, functions, class methods, class properties, constants, and parameters. Attributes do nothing during runtime.Attributes have no impact on the code, but available for the reflection API. Attributes in PHP 8 allow other code to examine the class properties and methods.We can have more than one attribute to a declaration.It may resolve the class name.Attributes can be namespaced.It may have zero or more parametersPHP 8 Attribute SyntaxIn PHP 8, #[ ] (# and square brackets) is used for an attribute declaration.We can declare multiple attributes inside #[ ], ... Read More

Reading Attributes with Reflection API in PHP 8

Urmila Samariya
Updated on 01-Apr-2021 06:21:56

576 Views

In PHP 8, we use classes, properties, and class constants, methods, functions, parameters to access the attributes.In PHP 8, Reflection API delivers the getAttribute() method on every matching Reflection object.The getAttribute() method returns an array of ReflectionAttribute illustrations that can be asked for attribute name, arguments and to instantiate an instance of the signified attribute.Example − Reading Attributes with the Reflection API in PHP 8OutputArray (    [Reading] => Array    (    )    [Property] => Array    (       [type] => function       [name] => Student    ) )

Number comparisons in PHP 8

Urmila Samariya
Updated on 01-Apr-2021 06:21:16

234 Views

When we compare a numeric in PHP 8, it will use number comparison. Else it will convert the number to a string and will use the string comparison.The string can be categorized in three ways −A string that contains only numeric. Example − 1234 or 1.24e1.A leading–numeric string − A leading string starts with a numeric string but it should be followed with non-numeric characters including the white space. Example − 12xyz or “123”Non-numeric string − The string which cannot be numeric and also a non-leading numeric string.Example − PHP 70=='foo' // PHP 7 will return true.Example − PHP 80 ... Read More

Advertisements