Pular para o conteúdo principal

Postagens

Mostrando postagens de 2023

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...

SQL: Never uses LIKE unless stricted necessary.

Demand MUST use "=" (equals sign) instead "LIKE" (like command) whenever "%" (percent sign) is NOT necessary Description In MySQL, the " LIKE " operator behaves similarly to " = " (equals sign) when the " % " (percent sign) wildcard is not utilized. Employing " LIKE " in most of the modern SGDBs (e.q. MySQL) results in the use of a more complex query structures. The examples below illustrate this. The first query does not use the " % " modifier and is compared to the second, which does. Both provide the same search structure. Contrasting these with the last query, which uses only " = ", results in a simpler search structure and all have the same output result. Therefore, if the " % " modifier is not being used in the search, it's preferable to utilize " = " for simpler and more efficient query execution. Examples 1: explain SELECT COUNT(1) FROM users WHERE `username` lik...