What are Weak Maps in PHP

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

436 Views

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 to objects, without leaking any memory.In other words, a weak map in PHP 8 is a group of objects in which keys weakly reference. A weak map uses a class to create an object which can be used as a key, which can help to remove and destroy the weak ... Read More

What is Trailing Comma in PHP

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

2K+ 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

Nullsafe Operator in PHP 8

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

1K+ 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

772 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

Union Type in PHP 8

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

837 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

660 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

321 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

Run PowerShell Script from Command Prompt

Chirag Nagrekar
Updated on 30-Mar-2021 14:05:03

23K+ Views

To run the PowerShell script from the command prompt, we can use the below command.ExampleFor example, we have a script TestPS.ps1 which first starts the spooler service and then copies a file to a different location. We need to call this script using the command prompt.C:\> PowerShell.exe -command "C:\temp\TestPS.ps1"The above command is similar to running individual PowerShell commands. Here we are providing the path of the script.OutputC:\>PowerShell.exe -command "C:\temp\TestPS.ps1" VERBOSE: Performing the operation "Start-Service" on target "Print Spooler (Spooler)". Status Name DisplayName ------ ----- ---------- Running Spooler Print Spooler VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\EnvVariable.txt Destination: ... Read More

Delete Hidden Files and Folders Using PowerShell

Chirag Nagrekar
Updated on 30-Mar-2021 14:04:01

6K+ Views

If we want to delete the hidden files and folders from the C:\temp on the local computer, we need to use the command shown in this example.ExampleBut first, the below command helps us to retrieve the hidden files and folders from the C:\temp.Get-ChildItem C:\Temp -Hidden -RecurseWe just need to pipe the Remove-Item command and to remove forcibly use -Force parameter.Get-ChildItem C:\Temp -Hidden -Recurse | Remove-Item -Force -VerboseOutput

Advertisements