When testing for the identity of strings using the strcmp() function has worse performance than the === operator. (3-5x)
Replace all ~300 calls to strcmp() with the operator.
Attention: This is replacement only works when the return value of strcmp() is checked for being a boolean value, otherwise a string ordering is performed and the call MUST NOT be replaced!
The operator is also slower than the = operator, so use the latter one if possible.
[TASK] Replace all strcmp() calls with ===
This patch replaces about 300 places using strcmp()
in the whole core. There are different contexts for strcmp()
within this set, i.e. checking for strings being '0',
'', not '' and the like.
strcmp() has to stay when it comes to real sorting of strings,
which is a rather rare case, otherwise it can be replaced with
The following 'rules' were used for the replacement:
- Use a type cast if you can't be sure about the incoming values.
We do not need type casts if the types are implicitly defined
before by another function. i.e. intval(), trim(), substr()
- Use int-cast whenever the values to be compared are numbers only.
- Use string-cast for any other combination. i.e
(string)$len === '0' when $len can be NULL, which is different
to (int)$len === 0
[BUGFIX] Adding new records in edit mode broken
It was not possible anymore to use the "add new record" button
in TCA select fields on records with pid > 0.
This patch makes it work again; the redirect to the original page
gets prevented when a pid is set. This was handled the same way
in 4.6 and 6.1.
#6 Updated by Jo Hasenau almost 7 years ago
Attention: This is replacement only works when the return value of strcmp() is checked for being zero, otherwise a string ordering is performed and the call MUST NOT be replaced!
Shouldn't it be: "is checked for being zero or not being zero"
Actually strcmp is meant to be used for sorting of strings, since it can return positive or negative values for cases when string A is "larger" or "smaller" than string B. It will return 0 when string "A" and string "B" are equal.
So any case that is just checking for the result of strcmp being TRUE or FALSE could IMHO be safely replaced.
In some cases we might have to add isset() before the === operator since NULL could be !== '' but still might have to be considered ''.
isset($foo) && $foo !== ''
will perform better, since it will just skip the operator in cases when $foo is NULL and it will no create any copies, so the memory footprint will be significantly smaller.