Pular para o conteúdo principal

PHP: Always use an index to add a values to an array

 

Demand

Always identify the array index when manipulating the array directly


Description

Whenever you are adding a new item to the array you must add an index so that php does not need to recalculate the size of the array to identify in which position the new content will be added.

Examples

1:  # Bad code:  
2:  do {  
3:    $array[] = $newItem;  
4:  } while(!feof($fp));  
5:    
6:  # Good code  
7:  $array = [];  
8:  $index = 0;  
9:  do {  
10:    $array[$index++] = $newItem;  
11:  } while(!feof($fp));  


Examples Explanation

When instantiating a new item in an array in the "bad example" php will identify that you expect the item to be added to the end of the array, since array is a primitive type and has no attributes php always needs to call a calculation function to identify the current size of the array before trying to add a new item to the end of it.

By monitoring the growth of the array in an external variable and automatically reference which position will be the new position, it speeds up the instantiating as it avoids constantly recalculating the size of the array. As seen in the "good sample".

Comentários

Postagens mais visitadas deste blog

Demystifying MySQL ALTER TABLE ALGORITHM: INPLACE vs INSTANT vs COPY

  Schema evolution is one of the trickiest aspects of database management. For developers and DBAs, the dreaded ALTER TABLE command often conjures fears of downtime, locks, and performance bottlenecks. But MySQL offers a powerful word! The "ALGORITHM" modifier that determines how schema changes are executed. In this article, we’ll break down the three key algorithms "INPLACE", "INSTANT", and "COPY" to help you understand their trade-offs, performance implications, and even some unconventional ways to use them. Whether you’re optimizing for zero downtime or experimenting with stress testing, this guide will give you practical insights and real-world strategies. IMPORTANT: Check your MySQL version: In MySQL 8.0+, many table changes are instantaneous, and the command modifications presented here are only available in this and later versions. Understanding the ALGORITHM Modifier When you run an ALTER TABLE in MySQL, you can specify the algorithm, lik...

Article: Preventing Database Gridlock: Recognizing and Resolving Deadlock Scenarios

Learn how deadlocks occur in database systems, understand their impact on performance, and discover practical techniques for identifying potential deadlock scenarios in your SQL code.

Collation Confusion: Demystifying VARBINARY vs VARCHAR in Database Design

Have you ever wondered why your database sorts strings differently than expected, or why a seemingly simple query delivers "quirky" results? If you’ve worked with databases like MySQL, PostgreSQL, or SQL Server, you’ve likely stumbled across the mysterious world of collation and the subtle but critical differences between VARBINARY and VARCHAR data types. In this article I hope to unravel these concepts for you, explore how collation shapes database behavior, and dive into an experimental case study to reveal surprising insights. Whether you’re a developer or a database engineer, this deep dive will equip you with practical knowledge to avoid common pitfalls and optimize your database designs. Let’s get started! What Is Collation and Why Does It Matter? Collation refers to the set of rules a database uses to compare and sort character strings. It governs how strings are ordered (e.g., is “Apple” less than “apple”?), how accents are treated (e.g., does “é” equal “e”?), and eve...