PHP Master of the Road (iii) _php Foundation

Source: Internet
Author: User
Tags php code php programming
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&GT;, B>, $string).

}
?>

This code tests how fast Ereg_replace is running




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 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 you can 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 turn to 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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.