1.2.663. Make One Call With Array¶
Avoid calling the same functions several times by batching the calls with arrays.
Calling the same function to chain modifications is slower than calling the same function once, with all the transformations at the same time. Some PHP functions accept scalars or arrays, and using the later is more efficient. Potential replacements :
Function |
Replacement |
str_replace() str_ireplace() substr_replace() preg_replace() preg_replace_callback() |
str_replace() str_replace() substr_replace() preg_replace() preg_replace_callback_array() |
<?php
$string = 'abcdef';
//str_replace() accepts arrays as arguments
$string = str_replace( ['a', 'b', 'c'],
['A', 'B', 'C'],
$string);
// Too many calls to str_replace
$string = str_replace( 'a', 'A', $string);
$string = str_replace( 'b', 'B', $string);
$string = str_replace( 'c', 'C', $string);
// Too many nested calls to str_replace
$string = str_replace( 'a', 'A', str_replace( 'b', 'B', str_replace( 'c', 'C', $string)));
?>
1.2.663.1. Connex PHP features¶
1.2.663.1.1. Suggestions¶
Use str_replace() with arrays as arguments.
Use preg_replace() with arrays as arguments.
Use preg_replace_callback() for merging multiple complex calls.
1.2.663.1.2. Specs¶
Short name |
Performances/MakeOneCall |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Major |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Examples |
|
Available in |