Found 1046 Articles for PHP

Uniform variable syntax in PHP 7

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

321 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

442 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

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

457 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

212 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

Generator delegation in PHP 7

Urmila Samariya
Updated on 13-Mar-2021 06:28:09

363 Views

The concept of generator is not new to PHP 7, because it was available in the earlier versions too. With generators, implementation becomes easy without the overhead of implementing a class that implements the iterator interface. With the help of a generator, we can write foreach code without using an array in memory. It also helps to eliminate the “exceed memory limit errors”.With the help of generator delegation in PHP 7, we can delegate to another generator automatically. It also allows arrays and objects that implement the traversable interface.Generator delegation Example 1Live Demo PHP 7 : Tutorialpoint ... Read More

Generator Return Expressions in PHP 7

Urmila Samariya
Updated on 13-Mar-2021 06:18:57

287 Views

In the previous versions of PHP, generator functions were not able to return expressions, but from the PHP 5.5, generator return expression is added in the existing one. By using generator return expressions, it is easy to use the return statement within a generator and, it also returns the value of the final expression.By using the generator return expression, we can only return the value of the expression but cannot return the reference. By using the new Generator::getReturn() method, we can fetch the value which can be used once the generator function has finished yielding the defined values.Using PHP 7 ... Read More

Types of Group Use declarations in PHP 7

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

149 Views

PHP 7 uses three different types of Group Use declarations −Non-mixed-use declarationsMixed-use declarationsCompound use declarationsNon-mixed-use declarations:Non-mixed-use declaration means we do not use the classes, functions, and constructs in a single statement. Or, we can say that when we declare classes, functions, and constants separately using a use statement. It is called Non-mixed group use declarations.Exampleuse Publishers\Packt\{ Book, Ebook, Video, Presentation }; use function Publishers\Packt\{ getBook, saveBook }; use const Publishers\Packt\{ COUNT, KEY };Mixed group use declarationsWhen we combine the PHP class, function and constants in a single-use statement, it is called a mixed group use declaration.Exampleuse Publishers\Packt\ {    Book, ... Read More

Group Use declarations in PHP 7

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

688 Views

In PHP 7, Group Use declaration is more readable and can be used to import classes, constants, and functions easily from the same namespace.Group Use declaration is used to import multiple structures easily from a namespace and cuts a good level of volubility in most cases. It is also useful to identify the multiple imported entities which belong to the same module.Example 1The following example shows the code before PHP 7 −Example 2The following example shows the code for PHP7 or PHP 7+use com\India\{ClassX, ClassY, ClassZ as Z}; use function com\India\{fn_x, fn_y, fn_z}; use const com\India\{ConstX, ConstY, ConstZ};ExplanationIn Example 1, ... Read More

Difference Between For and Foreach in PHP

AmitDiwan
Updated on 02-Mar-2021 05:08:19

3K+ Views

In this post, we will understand the differences between 'for' and 'foreach' loops in PHP −The 'for' loopIt is an iterative loop that repeats a set of code till a specified condition is reached. It is used to execute a set of code for a specific number of times. Here, the number of times is the iterator variable.Syntax:for( initialization; condition; increment/decrement ) {    // code to iterate and execute }Initialization: It is used to initialize the iterator variables. It also helps execute them one at a time without running the conditional statement at the beginning of the loop's condition.Condition: ... Read More

Advertisements