Found 1060 Articles for PHP

Generator delegation in PHP 7

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

480 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

388 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

256 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

914 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

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

Difference Between PHP and JavaScript

Kiran Kumar Panigrahi
Updated on 28-Jul-2022 12:42:36

8K+ Views

JavaScript and PHP are two of the most widely used and flexible computer languages that are used for building websites.PHP is currently the most widely used scripting language for server-side development, whereas JavaScript is a client-side scripting language. PHP is responsible for handling things on the server side, whereas JavaScript is responsible for handling things on the client side of the browser.Since PHP is derived from the C programming language, it is quite simple to learn for anyone who already possesses a solid foundation in C. Although both are used on websites in order to enhance their functionality, one of ... Read More

PHP Pushing values into an associative array?

AmitDiwan
Updated on 20-Nov-2020 05:38:42

2K+ Views

To push values into an associative array, use the brackets [] []. At first create an associative array −$details= array (    'id' => '101',    'name' => 'John Smith',    'countryName' => 'US' );The PHP code is as follows to insert values −Example Live Demo OutputArray (  [studentDetails] => Array (     [0] => Array (        [id] => 101 [name] => John Smith [countryName] => US       )    ) )

Extract numbers after the last hyphen in PHP?

AmitDiwan
Updated on 20-Nov-2020 05:36:38

846 Views

Let’s say the following is our string including hyphen and numbers as well −"John-45-98-78-7898906756"To extract numbers after – i.e. hyphen, use the concept of explode() with parameters - and $yourVariableName.The PHP code is as follows −Example Live Demo Output7898906756

PHP How to cast variable to array?

AmitDiwan
Updated on 20-Nov-2020 05:35:37

642 Views

To cast variable to array, use the below syntax −$yourNewVariableName=(array)$yourVariableName;The PHP code is as follows −Example Live Demo OutputArray ( [0] => Mike [1] => Sam [2] => David )

PHP How to display array values with text “even” to be displayed for even index values

AmitDiwan
Updated on 20-Nov-2020 05:34:16

146 Views

For this, you can use for loop along with some conditions.The PHP code is as follows −Example Live Demo OutputEven 1 Even 3 EvenAbove, for 0th index, the text “Even” is displayed and the same goes on for 2th and 4th index.

Advertisements