1. Use embedded HTML code, not PHP's echo statement. Because PHP is an embedded web programming language, you can embed HTML code and PHP code into each other. But many programmers are concerned about the excessive use of "" embedded PHP code in the HTML code will invoke the PHP interpreter multiple times, thus reducing the speed of PHP code, so prefer to use the PHP echo statement to output HTML code, rather than directly using HTML code. But the exact opposite is true. Each PHP page only calls once PHP interpreter to explain all the PHP code, so, only when needed to embed PHP code, and most of the time directly using the HTML code input results, not only will not reduce the speed of the program, but also because of the reduction of the Echo statement parsing, You can often improve the speed of your code.
The following piece of code proves our conclusion. In this code, we use the time test function described earlier.
2. Use Str-replace instead of Ereg-replace Programmers who are accustomed to programming with Perl are more willing to use ereg_replace to complete string substitution, because ereg_replace usage in PHP is similar to that of pattern matching in Perl. However, the following code demonstrates that using Str_replace instead of ereg_replace can greatly improve the speed of your code.
Test Str_replace and ereg_replace running speed
This code tests how fast Str_replace is running
emphasis;?>.
For ($i=0 $i<1000; $i++) {
Str_replace (I>, B>, $string).
Time to use Ereg_pattern-
Run the above code and get the result:
Time to use Str_replace-0.089757
Time to use Ereg_pattern-0.248881
From the results of the run 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 quotes ("") to refer to strings, or to use single quotes (). But in PHP, if you use double quotes to refer to a string, the PHP parser will first parse the string for a reference to a variable, and then replace the variable with a variable. If it's a single quote, it's not so complicated--directly displays all the strings that are enclosed in single quotes. Obviously, in PHP programming, using single quotes to refer to string variables is quicker than using double quotes.
4. Avoid using federated operations in the database PHP's database functions are powerful compared to other Web programming languages. But in PHP, the database operation is still a very time-consuming and laborious thing, so as a web programmer, to minimize the database query operations, but also to establish an appropriate index for the database. Another noteworthy thing is that when using PHP to manipulate a database, it is possible to not use a joint operation with multiple tables, although joint operations can enhance the query function of the database, but greatly increase the burden on the server.
To illustrate this, let's take a look at this simple example below.
We created two datasheet foo and Big_foo in the database. In Datasheet foo, there is only one field that contains all the natural numbers from 1-1000. The datasheet 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, we can see that for two data tables with 1000 records, the speed is no faster than the single operation of a large data table with 1 million records.
5. Note the difference between include and require In PHP, include () and require () have the same functionality, but there are some differences in usage, include () is a conditional inclusion function, and require () is an unconditional inclusion function. For example, in the following example, if the variable $somgthing is true, the file Somefile will be included:
if ($something) {
Include ("Somefile");
}
But no matter what value the $something takes, the following code will include the file Somefile in the file:
if ($something) {
Require ("Somefile");
}
The following interesting example fully 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 the code wants to include different files in each loop. If you want to complete this function, you must help the function
Include () {
$i = 1;
while ($i < 3) {
Include ("somefile.$i");
$i++;
}
6. Note the difference between Echo and print The functions of ECHO and print are basically the same in PHP, but there is a slight difference between the two. You can use print as a normal function in your PHP code, 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 in code than the print statement, because the Echo statement does not require any numeric values to be returned.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.