Use Str-replace instead of Ereg-replace
Programmers who are accustomed to programming with Perl are more likely to use ereg_replace to do string substitution work, because the use of ereg_replace in PHP is similar to that of pattern matching in Perl. However, the following code proves that using Str_replace instead of ereg_replace can greatly improve the speed of your code.
Test the operating speed of str_replace and ereg_replace
This code tests the speed at which the Str_replace runs
emphasis;?>
for ($i=0; $i<1000; $i++) {
Str_replace (I>, B>, $string).
}
?>
This code tests the speed at which the ereg_replace runs
for ($i=0; $i<1000; $i++) {
Ereg_replace (< ([/]*) i>, <\\1b>, $string).
}
?>
Print results
Conclusion
Time to use Str_replace-
Time to use Ereg_pattern-
Run the above code to get the result:
Time to use Str_replace-0.089757
Time to use Ereg_pattern-0.248881
From the running results we can see that using str_replace instead of ereg_replace as a string substitution function greatly improves the speed of the code.
3. Note the reference to the string
PHP, like many other programming languages, can use double quotation marks (\ "\") to refer to a string, or you can use single quotation marks (). In PHP, however, if you use double quotation marks to refer to a string, the PHP parser will first parse the string with a reference to the variable and replace it with a variable. If it's a single quote, it's not so complicated--just display all the strings that enclose the single quotation marks directly. Obviously, in PHP programming, it is quicker to use single quotation marks to refer to string variables than to use double quotes.
4. Avoid the use of federated operations in the database
PHP has a very powerful database function compared to other Web programming languages. However, in PHP the operation of the database is still a very time-consuming task, so, as a web programmer, to minimize the database query operations, and should establish a proper index for the database. Another notable thing is that when working with a database in PHP, it is possible not to use multiple data tables for federated operations, although federated operations can enhance the query functionality of the database, but it greatly increases the burden on the server.
To illustrate this problem, we can look at this simple example below.
We created two data tables Foo and Big_foo in the database. In the data table Foo, there is only one field that contains all the natural numbers from 1-1000. The data table Big_foo also has only one field, but contains all the natural numbers from 1-1 to 000,000. So, in terms of size, Big_foo equals foo with its own joint operation.
$db->query (\ "SELECT * from foo\");
0.032273 secs
$db->next_record ();
0.00048999999999999 secs
$db->query (\ "INSERT into Foo values (NULL) \");
0.019506 secs
$db->query (\ "SELECT * from Foo as a, foo as b\");
17.280596 secs
$db->query (\ "SELECT * from Foo as a, foo as B where a.id > b.id\");
14.645251 secs
$db->query (\ "SELECT * from Foo as a, foo as b where a.id = b.id\");
0.041269 secs
$db->query (\ "SELECT * from big_foo\");
25.393672 secs
From the above results we can find that two data tables with 1000 records are combined at a speed that is no faster than a single 1 million-record large data table.
5. Note the difference between include and require
In PHP, the Include () function is the same as require (), but there are some differences in usage, include () is conditional include function, and require () is an unconditional include function. For example, in the following example, if the variable $somgthing is true, it will contain the file Somefile:
if ($something) {
Include (\ "somefile\");
}
However, regardless of the $something value, the following code will include the file somefile into the file:
if ($something) {
Require (\ "somefile\");
}
The following interesting example illustrates the difference between the two functions.
$i = 1;
while ($i < 3) {
Require (\ "somefile.$i\");
$i++;
}
In this code, every time a loop is made, the program will include the same file. Obviously this is not the intention of the programmer, and from the code we can see that this code wants to include different files in each loop. If you want to complete this function, you must ask for the function include ():
$i = 1;
while ($i < 3) {
Include (\ "Somefile.$i\");
$i++;
}
6. Notice the difference between Echo and print
The functions of Echo and print in PHP are basically the same, but there are subtle differences between the two. In PHP code, print can be used as a normal function, for example, after executing the following code, the value of the variable $res will be 1.
$ret = print \ "Hello world\";
This means that print can be used in some complex expressions, while Echo is not. Similarly, the Echo statement runs slightly faster than the print statement in the code, because the Echo statement does not require any values to be returned
The above describes the Mo Yanlin China good Sound Ordinary way PHP Master Road (iii), including the Mo Yanlin of China's sound ordinary road aspects of the content, I hope that the PHP tutorial interested in a friend helpful.