Urmila Samariya has Published 145 Answers

PHP 8 – How to use ValueError to check if the value encountered is of correct type?

Urmila Samariya

Urmila Samariya

Updated on 01-Apr-2021 06:57:44

PHP 8 uses a new built-in exception ValueError. PHP throws this exception when we pass a value to a function, which has a valid type but cannot be used for operation. In the earlier versions of PHP, we used to get a Warning error in such cases, but PHP 8 ... Read More

How the Non-Capturing Exception Catches work in PHP 8?

Urmila Samariya

Urmila Samariya

Updated on 01-Apr-2021 06:50:36

In the previous versions of PHP, if we wanted to catch an exception, then we needed it to store in a variable to check whether that variable is used or not.Before PHP 8, to handle the exception catch block, we needed to catch the exception (thrown by the try block) ... Read More

str_starts_with and str_ends_with function in PHP 8

Urmila Samariya

Urmila Samariya

Updated on 01-Apr-2021 06:44:23

str_starts_with and str_ends_with function are added in PHP 8 to check if a given string starts or ends with another string or not. If it starts and ends with another string, it returns true, otherwise false.Examplestr_starts_with('hello haystack', 'hello'); //starts string found 'True' str_starts_with('hello haystack', 'stack'); //ends string found 'True'str_starts_with('hello haystack', ... Read More

PHP 8 – Using str_contains() to check if a string contains a substring

Urmila Samariya

Urmila Samariya

Updated on 01-Apr-2021 06:43:17

In PHP 8, str_contains function determines if a string contains a given substring anywhere. The str_contains function checks if a first-string is contained in the second string and it returns a true /false Boolean value based on whether the string is found or not. it is a self-explanatory function.str_contains(string $haystack, ... Read More

What are Weak Maps in PHP?

Urmila Samariya

Urmila Samariya

Updated on 01-Apr-2021 06:42:10

Weak Maps were added in PHP 7.4. It can be used to remove or delete objects when the cache refers to objects entity classes. It references to those objects, which does not avoid objects from memory garbage collected. In PHP 8, weak maps allow us to store random data linked ... Read More

What is Trailing Comma in PHP?

Urmila Samariya

Urmila Samariya

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

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 ... Read More

Nullsafe Operator in PHP 8

Urmila Samariya

Urmila Samariya

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

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 ... Read More

Match Expression in PHP 8

Urmila Samariya

Urmila Samariya

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

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 ... Read More

Union Type in PHP 8

Urmila Samariya

Urmila Samariya

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

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 Type<?php    class Number    {   ... Read More

Attributes in PHP 8

Urmila Samariya

Urmila Samariya

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

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 ... Read More

Advertisements