Php programmers must look at php optimization efficiency (1/4)

Source: Internet
Author: User
Tags foreach constant php code php tutorial

7.1 when file_get_contents can be used to replace file, fopen, feof, fgets, and other methods, file_get_contents should be used as much as possible because of its high efficiency! Note the php Tutorial version when file_get_contents opens a url file;
2. Perform as few file operations as possible, although php file operations are not efficient;
3. Optimize select SQL statements and perform insert and update operations as little as possible (I have been maliciously approved in update );
4. Try to use php internal functions as much as possible (but in order to find a function that does not exist in php, it is a waste of time to write a user-defined function, experience problems !);
5. Do not declare variables inside the loop, especially big variables: objects (isn't it just a matter of attention in php ?);
6. Do not use nested values for multi-dimensional arrays;
7. Do not use regular expressions when using internal php strings to operate functions;
8. foreach is more efficient. Use foreach instead of the while and for loops;
9. Use single quotes instead of double quotes to reference strings;
10. Replace I = I + 1 with I + = 1. In line with the c/c ++ habits, high efficiency ";
11. For global variables, unset () should be used up;
12. In for or foreach, replace $ temparray [] = $ field with $ temparray [count ($ temparray)] = $ field
13. static call members must be defined as static (php5 only)
Tip: php 5 introduces the concept of static members, which is used in the same way as the static variables in php 4 functions, but the former is used as a member of the class. Static variables are similar to class variable in ruby. All class instances share the same static variables.

Quote:
Copy the code as follows:

// Php code highliting for cu by dz902
<? Php
Class foo {
Function bar (){
Echo 'foobar ';
}
}
$ Foo = new foo;
// Instance way
$ Foo-> bar ();
// Static way
Foo: bar ();
?>

Static calling uses non-static members, which is 50-60% slower than static calling. This is mainly because the former generates an e_strict warning and the internal switch is also required.
Use a class constant (php5 only)
Tip: php 5's new features are similar to c ++'s const.
The benefit of using a class constant is:
-No additional overhead for parsing during compilation
-The pooled tables are smaller, so internal search is faster.
-The class constant only exists in the specified "namespace", so the name of the miscellaneous is shorter.
-Code cleaner to make debugging easier
(Temporarily) do not use require/include_once
Require/include_once will open the target file every time it is called!
-If the absolute path is used, php 5.2/6.0 does not have this problem.
-The new apc cache system has solved this problem.
File I/o increase => efficiency reduction
If necessary, you can manually check whether the file has been require/include.
Do not call meaningless functions
Do not use functions when there are corresponding constants.
Quote:
Copy the code as follows:
// Php code highliting for cu by dz902

<? Php
Php_uname ('s ') = php_ OS;
Php_version () = php_version;
Php_sapi_name () = php_sapi;
?>

Homepage 1 2 3 4 Last page

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.