PHP first tip. Do not use @

@ Operator is used to hide errors when using or carrying out certain functions. With this statement it is not necessary to use for two reasons: it does not justify itself in speed, that is, slow; automatically blocks all the errors that occurred during execution, there are undesirable.

Forget about cp1251

PHP important tip. Anyone who still works with the coding cp1251 – immediately go to utf8 !!!Because many functions for working with strings only supports utf8. And this is just one of the reasons.

Read phpmanual

Developers have created a huge amount of functionality, which is a sin not to take advantage, but in order to know about all the virtues of php read phpmanual developers.

The best code – where it is less

01.<font><font><? Php</font></font><font></font><font><font>
02.// Long code</font></font><font></font><font><font>
03.if ($ a <b) {</font></font><font></font><font><font>
04.$ Msg = 'a <b';</font></font><font></font><font><font>
05.} Else {</font></font><font></font><font><font>
06.$ Msg = 'a> b';</font></font><font></font><font><font>
08.// Analogue short code</font></font><font></font><font><font>
09.$ Msg = (a <b)? </font><font>'A <b': 'a> b';</font></font><font></font><font><font>

Use single quotes

Single quotes faster than a double, because PHP searches for variables inside strings, enclosed in double quotes. In addition, a string enclosed in single quotes are perceived visually better. When you want to pass a string variable, the best performance use the line, framed by double quotes.

When you create a medium-large in size of files , you need to take into account the performance of each line, each function, for when the great script, it can affect the abuse of server resources, there is not very good.

A simple example of optimization:

Echo faster than print

As often as possible frees memory

require_once is not so good

str_replace faster than preg_replace

Avoid regex, better use of string functions, they are faster

As much as possible Cache

Not everything has to be OOP

mod_gzip – Apache module that allows you to compress your data on the fly and can reduce the amount of data up to 80%

What is the release of memory?

When working with large amounts of data in the course of the script created a considerable number of variables. Therefore, at some point you need to delete variables, making removal of the concept of a variable can be compared more with cleaning variable using unset () function.

It is also advisable to use a minimum number of variables, for specific instructions. Because the vast abundance of variables reduces the readability of the code.

Consider the following example:

1.<font><font>// So it is not necessary to write</font></font><font></font><font><font>
2.$ Time = time ();</font></font><font></font><font><font>
3.$ Login = $ _SESSION [ 'login'];</font></font><font></font><font><font>
4.$ Hash = md5 ($ login $ time.);</font></font><font></font>
6.// If it can be written as</font></font><font></font><font><font>
7.$ Hash = md5 ($ _ SESSION [ 'login'] time ().);</font></font><font></font>

First write, then optimize

This PHP advice applies almost everywhere and in everything (this applies not only PHP, and programming in general). Not advisable to optimize, when the script is in the process of development. You do not see the whole picture as a whole.

How, though, it is appropriate, but I’m always sketching out a rough working vari-ant, and then start to analyze and add or remove code elements. It is also important to think things through and initially write a sensible draft, otherwise it makes sense to write, if you then have to redo everything from scratch.

The perennial problem of novice coders is that when they sorted array in the loop for, they used in each iteration count () function, which counts the number of elements in the array. Which again affects the speed scenario.


1.<font><font>for ($ i = 0; $ i <count ($ array); $ i ++) {</font></font><font></font><font><font>
2.// statements</font></font><font></font><font><font>

Where better to bring in some kind of a variable number of elements in the array and do not touch it.

1.<font><font>$ Elements = count ($ array);</font></font><font></font><font><font>
2.for ($ i = 0; $ i <$ elements; $ i ++) {</font></font><font></font><font><font>
3.// statements</font></font><font></font><font><font>

Always comes a time when you begin to work with strings, namely when it is necessary to compare the length of string with a constant. And then we immediately turn to strlen () functions, koto-paradise returns the number of characters per line. This in itself is fast, because the length is already known, and function just retrieves it.

Consider this example, which we then rewrite more optimized.

1.<font><font>if (strlen ($ string) <10) {echo "String is short"; </font><font>}

Optimized version:

1.<font><font>if (isset ($ string {10})!) {echo "String is short"; </font><font>}

Let’s talk about increments:

The growth rate of the global variable is two times slower than a local.

Increment properties of the object (ie $ this-> inc ++) is three times slower than a local variable.

Increment uncertain variable is 9-10 times slower than a pre-initialized.

The growth rate of the local variable in the method – the fastest. Nearly also works increment a local variable in the function.

(Visited 57 times, 1 visits today)
adminTipsphp tips
PHP first tip. Do not use @ @ Operator is used to hide errors when using or carrying out certain functions. With this statement it is not necessary to use for two reasons: it does not justify itself in speed, that is, slow; automatically blocks all the errors that occurred...

Do you want to be notified about new DBA updates, releases, jobs and free tips? Join our email newsletter. It's fast and easy. You will be among the first to know about hot new DBA updates and stuff, it will help you enhance your DBA skills.
We take your privacy very seriously