How to improve the programming efficiency of PHP programming and improve the _php instance of PHP programming technology

Source: Internet
Author: User
Tags comments echo command php programming switch case

It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for variables in a string enclosed in double quotes, single quotes are not, note: only echo can do this, it is a "function" that can take multiple strings as arguments: ECHO is the language structure, not the real function, so the function is added double quotes.

1, if you can define the method of the class as static, as far as possible to define static, it will increase the speed of nearly 4 times times.

2, $row [' ID '] speed is $row[id] 7 times times.

3, ECHO is faster than print, and uses Echo's multiple arguments (to refer to a comma rather than a period) instead of a string connection, such as Echo $str 1, $str 2.

4, before executing the For loop to determine the maximum number of loops, do not calculate the maximum value per cycle, preferably using foreach instead.

5, the cancellation of those unused variables, especially large arrays, in order to free memory.

6, try to avoid the use of __get,__set,__autoload.

7, require_once () costly.

8, include files as far as possible using the absolute path, because it avoids the PHP to include_path to find the file speed, parsing the operating system path requires less time.

9, if you want to know the script to start execution (that is, the server to receive client requests) at the moment, using $_server[' request_time ' is better than time ().

10. Functions do the same function instead of regular expressions.

11, the Str_replace function is faster than the Preg_replace function, but the efficiency of the STRTR function is four times times that of the Str_replace function.

12. If a string substitution function can accept an array or character as a parameter, and the parameter length is not too long, consider writing an extra paragraph of substitution code so that each pass parameter is a character, rather than writing a single line of code to accept the array as a query and replacement parameter.

13. Using the Select Branch statement (switch case) is better than using multiple if,else if statements.

14, using the @ block error message is very inefficient, extremely inefficient.

15, open the Apache mod_deflate module, you can improve the browsing speed of the Web page.

16, the database connection should be turned off when use is finished, do not use long connection.

17. Error messages are costly.

18, in the method to increase the local variable, speed is the fastest. Almost as fast as calling a local variable in a function.

19, incrementing a global variable is twice times slower than incrementing a local variable.

20, incrementing an object property (such as: $this->prop++) is 3 times times slower than incrementing a local variable.

below to introduce you to improve the PHP programming technology.

I decided here to tell you some things that will improve your PHP code effect:

1, PHP tags

I know some people like to use the abbreviated label when writing PHP code-<.? But that's not a good habit, because the thumbnail tags are not recognized correctly on some servers, and the standard PHP tags allow you to compile your PHP code exactly on any server. Maybe one day you'll need to install your code on a server that doesn't support a thumbnail, so you'll have to spend one hours or more of your time sitting down to upgrade your PHP code.

2, Debugging of PHP code

Sometimes we run into problems running PHP code and we don't know where the problem is. PHP has a error_reporting () function that can tell you every error in your code. If you want it to display all the possible error messages for the page, place the following code in the second line of the file:

Php:

--------------------------------------------------------------------------------
Error_reporting (E_all);
--------------------------------------------------------------------------------

3. Debugging of PHP Code (supplemental)

If you complete a file with 1200 lines of PHP code and browse it in the browser, the error showing your code appears in line No. 561 of the file. Now that you have an easy way to find the line, follow these steps:

--Create a new Notepad
--Copy your PHP code into the
--"edit"-> "Go"
--Enter "561", carriage return
--your mouse is stuck in 561 lines.
--See if there is an error near the line
--fix bugs, upload code back to your space, and it's probably going to work. If there are any errors, repeat the steps above.
Hesitation A niche supplement: now most use the software such as EditPlus, this method is outdated OH

4, use the annotation

If your PHP code has 1200 lines, it's hard to figure out what it's going to do, and the solution is to annotate your code.
PHP annotations are different from the <!--HTML annotation--> in HTML because it will not be output (assuming they are not even seen when viewing source files)
There are three ways to add comments in PHP:

Php:

--------------------------------------------------------------------------------

<?php 
//Your comments//# Your comments//your comments/* 
?>

--------------------------------------------------------------------------------

You can decorate them as you wish, and you are the only one who uses them.

5, the PHP code indentation

I personally don't like to indent PHP code, but it does make code easier to read. When I had to indent, I used the tab to do the following:

Php:

--------------------------------------------------------------------------------
<?php/ 
/Settings// 
  $var 1 = "this";
Showing Variables// 
  if ($var 1 = "This") { 
    echo ' you said this '; 
  } else{ 
    echo "you said this"; 
  } 
? >
-------------------------------------------------------------------------------

6, modify your PHP file contains the way

I'm sure most of the people here will need to include one or two other files in one file. Have you ever thought about what you would do if you didn't have the file you needed? Does the person browsing your website feel that you are not professional enough?
In the PHP code I wrote, I made sure it existed before I included another file, as in the following example:

Php:

--------------------------------------------------------------------------------
<?php 
if (!file_ Exists ("layout.inc.php")) {exit ("Error:layout File Missing");} Else{include_once ("layout.inc.php"); 
>

7. Database Query

Sometimes your PHP code contains a connection to a database, and you may encounter minor problems, and most people who are prone to database problems write code in such a form:

Php:

--------------------------------------------------------------------------------
<?php 
mysql_query ( "INSERT into tablename (' id ', ' name ') VALUES (' 1 ', ' Mike ')"); 
? >

。。 After running, he found that the data was not plugged into the database, so we can solve the problem:

Php:

--------------------------------------------------------------------------------
<?php 
mysql_query ( "INSERT into tablename (' id ', ' name ') VALUES (' 1 ', ' Mike ')") or exit ("MySQL Error:".) Mysql_error ()); 
? >

8, abbreviations similar to if-then statement

If you receive data from a registration page and you want to make sure that all the information is filled out, you may use statements that contain many if-then formats, like this:

Php:

--------------------------------------------------------------------------------
<?php 
if (!$_post[ Name] {exit ("Sorry, but you did not fill-in all of the requested fields.");} 
if (!$_post[email]) {exit ("Sorry, but you did not fill-in all of the requested fields."); 
>

And you can actually combine the two lines of the If-then statement to make it only one line:

Php:

--------------------------------------------------------------------------------
<?php 
if (!$_post[ Name]) | | (!$_post[email]) {Exit ("Sorry, but you did not fill-in all of the requested fields."); 
>

|| Have the same meaning as or,&& and and respectively

9. Use Echo or print?

Most people would say "Echo and print are the same" and I agree with that. However, echo runs much faster than print and one letter less than print. The echo command appears later than print (I think), and obviously you know what to choose.

10. Input a large section of HTML language

I believe many people have solutions to this, but I would like to talk to you about some ways to solve this.
⑴, enter the end tag for PHP, and then you can enter the HTML code randomly, and then PHP's start tag (I don't like it because it looks unprofessional).
⑵, add a backslash in every HTML code (this works, but you always have to--every sentence).
⑶, using the Echo or print commands, that's all (recommended):

Php:

--------------------------------------------------------------------------------
<?php 
//showing a Huge chunk of HTML at a time// 
echo<<<end 
<font face= "Verdana" color= "Orange" size= "3" >large, Ora Nge Text in Font Size 3</font> 
<br><br> more HTML down here 
. 
<br><br> 
<div align= "Center" >centered text</div> end 
; 
? >
--------------------------------------------------------------------------------

I actually have a lot of other things to say about modifying the PHP code, but that's it, I don't want to keep bothering everyone.
Hope to help you.

The above content is small series to introduce the PHP programming development How to improve the programming efficiency of PHP programming technology, all the narration, I hope you like.

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.