Home > Article > Backend Development > Five PHP performance optimization tips
1. Don’t just copy variables
Sometimes in order to make the PHP code cleaner, some PHP newbies (including me) will copy the predefined variables into a variable with a shorter name. In fact, this is The result is doubled memory consumption, which only makes the program slower. Just imagine, in the following example, if the user maliciously inserts 512KB of text into the text input box, this will cause 1MB of memory to be consumed!
BAD:
GOOD:
2. Use single quotes for strings
PHP engine It is allowed to use single quotes and double quotes to encapsulate string variables, but there is a big difference! Using double-quoted strings tells the PHP engine to first read the contents of the string, find the variables in it, and change them to the values corresponding to the variables. Generally speaking, strings have no variables, so using double quotes will lead to poor performance. It is better to use string concatenation instead of double quoted strings.
BAD:
GOOD:
BAD:
GOOD:
);
; Many PHPprogrammers (including me) don’t know that when outputting multiple variables with stink, you can actually use commas to separate them, instead of concatenating them with strings first, as shown in the following number In one example, there will be a performance problem due to the use of connectors, because this will require the PHP engine to first connect all the variables and then output them. In the second example, the PHP engine will output them sequentially. them. BAD:
,
addUser();
}
deleteUser();
}
editUser();
}
defaultAction();
}
GOOD:
addUser( );
deleteUser();
;
;
:
;
}