Optimizing your code in PHP

30 August 2009 | In Web | 801 views | By

Click & share: 

phpAqui informamos algumas Tips rápidas para melhorar a sua codificação. Usually we programmers have vices that we acquired in day to day, without knowing, make our code slower. If we stay on the lookout for some minor details, our encoding becomes more agile and effective.

1. If a method can be static, declare it as static! THE performance aumenta 4 times;

2. Avoid using magic methods as _ _GET, __set and __autoload, If possible;

3. require_once() is expendable and demand enough memory;

4. Use full paths (full path) us includes and requires, because it's spent less time resolving the OS paths;

5. If you want to find out the time when the script started running, It is preferable to use $_SERVER[REQUEST_TIME ´ ´] than team();

6. See if you can use strncasecmp, strpbrk and stripos instead of regex;

7. str_replace is faster than preg_replacand, but strtr is 4 times faster than str_replace;

8. If a function, How to replace strings, accept both arrays as simple characters as arguments, and if your argument list is not very long, consider making a few redundant replacement statements, passing one character at a time, instead of a line of code that accepts arrays as search-and-replace arguments;

9. Error suppression with @ is very slow;

10. $row[´ ´ id] is 7 times faster than $row[id];

11. Messages de erro require more processing;

12. Do not use functions inside of loops, as for example:

for ($x=0; $x < count($array); $x)

The function count() is called every time the loop runs.;

13. Comment whenever your code. This greatly facilitates any maintenance.

14. ECHO is faster than PRINT. Claro que a difference é mínima. Because the print() prints a value on the screen and returns an integer value(1 or 0) While the echo just prints the value on the screen.

15. Set the size of their loops before running them. Try to avoid the use of functions such as count() and sizeof() in the middle of loops.

16. Whenever possible, free variables that are no longer used, mainly array's.

unset($frutas[0]); //para apagar um índice no caso de arrays

17. Search using single quotes for STRINGS. When you use single quotation marks, PHP doesn't bother to try to interpret what's inside the STRING (variables and etc. ..).

With double quotes you can do this:

$texto = “meu texto com uma $variavel.”;

I.e., When ever I have quotation marks, PHP will check if you have any variable to be interpreted. In the case of single quotation marks, She does not interpret variables inside her, so, PHP spends less time.

18. It is faster to use "SWITCH" than several "ELSE IF".

switch ($mes) {
case 1:
echo 'Janeiro';
case 2:
echo 'Fevereiro';
case 3:
echo 'Março';
case 4:
echo 'Abril';
case 5:
echo 'Maio';
echo 'Não Sei';

In Ivaiporã-PR, Computer engineer, Workgroup Administrator Tips in General. Passionate about technology and Informatics.

A bit about us

    The Group generally appeared in Tips 2007 from innovative ideas on troubleshooting problems faced daily by those who use the technology and computer science, both ordinary users and technicians. But where did, why and what is the purpose of this site?

Click here to read!

Siga o Dicas em Geral no Google+

Video of the week