Found 1060 Articles for PHP

Mixed Pseudo Type in PHP 8

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

738 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

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

Uniform variable syntax in PHP 7

Urmila Samariya
Updated on 13-Mar-2021 07:13:50

461 Views

In the older versions of PHP, we faced an inconsistency problem. For example: ${$first [‘name’]}. This syntax may create confusion or we can say that the syntax is not consistent. To overcome the inconsistency problem, PHP 7 added the new syntax called “Uniform variable syntax”.The uniform variable syntax evaluates the variables from left to right.We need to add the curly brackets to use the uniform variable syntax. For example, echo ${$first[‘name’]};The uniform variable syntax allows the combinations of operators and also it can break the backward compatibility in some expressions wherever older evaluations are used.ExampleLive DemoOutputOutput for the above PHP ... Read More

Unicode codepoint escape syntax in PHP 7

Urmila Samariya
Updated on 13-Mar-2021 07:11:29

637 Views

PHP 7 takes a Unicode codepoint in hexadecimal form as input and produces the outputs in UTF-8 character format within a double-quoted string. It could be any combination of hexadecimal digits, 2, 4, 6, and above. We can write Unicode characters by using a double-quoted or here docstring, without calling a function. Leading zero is optional in any hexadecimal form. UTF-8 Character Note: We can construct the full Unicode characters using “\u{xxx}” syntax.Some languages, for example, Hebrew and Arabic are read from right to left instead of left to right. We can reverse the text using the ... Read More

Display array structure and values in PHP 7

Urmila Samariya
Updated on 13-Mar-2021 07:06:12

14K+ Views

An array in PHP is a type of data structure that can store multiple elements of similar data type under a single variable.To display array structure and values in PHP, we can use two functions. We can use var_dump() or print_r() to display the values of an array in human-readable format or to see the output value of the program array.Difference between print_r and var_dumpprint_r: It is used to display the variable information in a human-readable format. Array values will be present in a format so that keys and elements can show. print_r also shows protected and private properties of ... Read More

Exceptions and Error in PHP 7

Urmila Samariya
Updated on 13-Mar-2021 07:03:46

513 Views

In the earlier versions of PHP, we could handle only exceptions. It was not possible to handle errors. In the case of a Fatal Error, it used to halt the complete application or some part of the application. To overcome this problem, PHP 7 added the throwableinterface to handle both exceptions and errors.Exception: Wherever a fatal and recoverable error occurs, PHP 7 throws an exception instead of halting the complete application or script execution.Error: PHP 7 throwsTypeError, ArithmeticError, ParserError, and AssertionError, but the warnings and notice errors remain unchanged. Using the try/catch block, error instance can be caught, and now, the ... Read More

Preg_replace_callback_array() in PHP 7

Urmila Samariya
Updated on 13-Mar-2021 06:29:55

291 Views

Preg_replace_callback_array() function in PHP 7 represents a regular expression and replaces the use of callbacks. This function returns a string or an array of strings to match a set of regular expressions and replaces them using a callback function.Syntaxpreg_replace_callback_array(patterns, input, limit, count)Parameter Values:pattern − It requires an associate array to associate the regular expression patterns to callback functions.input/subject −It requires an array of strings to perform replacements.limit −It is optional. -1 is used for default, which means it is unlimited. It sets a limit to how many replacements can be done in each string.count −It is also optional like the ... Read More

Advertisements