PHP functions, files, and SQL statement optimization methods

Source: Internet
Author: User

. If you can use file_get_contents to replace file, fopen, feof, fgets, and other methods, use file_get_contents as much as possible because of its high efficiency! Note the php Tutorial version when file_get_contents opens a url file;

<? Php
Echo file_get_contents ("test.txt ");
?>

The file_get_contents () function reads the entire file into a string.

Like file (), the difference is that file_get_contents () reads the file into a string.

The file_get_contents () function is the preferred method for reading the file content into a string. If supported by the operating system, the memory ing technology will be used to enhance the performance.

Syntax
File_get_contents (path, include_path, context, start, max_length) parameter description
Path is required. Specifies the file to be read.
Optional. If you want to search for files in include_path, you can set this parameter to "1 ".
Context is optional. Specifies the file handle environment.

Context is a set of options that can modify the behavior of a stream. If null is used, this parameter is ignored.
 
Start is optional. Specifies the location where the file starts reading. This parameter is newly added in php 5.1.
Max_length is optional. The number of bytes to read. This parameter is newly added in php 5.1.

Description


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 );

You can use lock table and unlock to insert instances in batches.

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 !);
Internal functions such as time, date, fopen, rsort, etc.

5. Do not declare variables inside the loop, especially big variables: objects (doesn't it seem to be a concern in php ?);

If for ($ I = 1; $ I <= 10; $ I ++) {$ a = 1 ;...}

6. Do not use nested values for multi-dimensional arrays;

7. Do not use regular expressions when you can use php internal 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. unset () should be used up for global variables;

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.