How to improve programming efficiency in PHP programming development, improve PHP programming technology, programming development and programming technology_PHP tutorial

WBOY
Release: 2016-07-12 09:05:17
Original
779 people have browsed it

How can PHP programming development improve programming efficiency? Improve PHP programming technology, programming development programming technology

Use single quotes instead of double quotes to include strings, it will be faster. Because PHP will search for variables in strings surrounded by double quotes, single quotes will not. Note: only echo can do this, it is a "function" that can take multiple strings as parameters (Annotation: PHP Manual It is said that echo is a language structure, not a real function, so the function is enclosed in double quotes).

1. If you can define a class method as static, try to define it as static, and its speed will be increased by nearly 4 times.

2. $row['id'] is 7 times faster than $row[id].

3. Echo is faster than print, and uses multiple parameters of echo (annotation: refers to using commas instead of periods) instead of string concatenation, such as echo $str1, $str2.

4. Determine the maximum number of loops before executing the for loop. Do not calculate the maximum value every loop. It is best to use foreach instead.

5. Unregister unused variables, especially large arrays, to free up memory.

6. Try to avoid using __get, __set, __autoload.

7. require_once() is expensive.

8. Try to use absolute paths when including files, because it avoids the speed of PHP searching for files in include_path, and the time required to parse the operating system path will be less.

9. If you want to know the time when the script starts executing (annotation: that is, the server receives the client request), it is better to use $_SERVER['REQUEST_TIME'] than time().

10. Functions replace regular expressions to complete the same function.

11. The str_replace function is faster than the preg_replace function, but the strtr function is four times more efficient than the str_replace function.

12. If a string replacement function accepts arrays or characters as parameters, and the parameter length is not too long, you can consider writing an additional replacement code so that each parameter passed is one character instead of just one line. The code accepts arrays as parameters for query and replace.

13. It is better to use a selective branch statement (translation annotation: switch case) than to use multiple if, else if statements.

14. Using @ to block error messages is very inefficient, extremely inefficient.

15. Opening the mod_deflate module of apache can improve the browsing speed of web pages.

16. The database connection should be closed when finished using it, and do not use long connections.

17. Error messages are expensive.

18. Increasing local variables in methods is the fastest. Almost as fast as calling local variables in a function.

19. Incrementing a global variable is 2 times slower than incrementing a local variable.

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

The following will introduce you to improve PHP programming technology.

I decided to tell you some things to note here that can improve the effectiveness of your PHP code:

1. PHP tag

I know that some people like to use abbreviated tags when writing PHP code - , but this is not a good habit, because abbreviated tags cannot be recognized correctly on some servers, and standard PHP tags The use of allows you to accurately compile your PHP code on any server. One day you may need to install your code on servers that do not support abbreviation tags, so you will have to spend an hour or more to sit down and upgrade your PHP code

2. Debugging of PHP code

Sometimes we encounter problems when running PHP code and we don't know where the problem lies. There is a special error_reporting() function in PHP, which can tell you every error in your code. If you want it to display all possible error messages on the page, you can put the following code on the second line of the file:

PHP:

------------------------------------------------- ----------------------------------
error_reporting(E_ALL);
-------------------------------------------------- -------------------------------

3. Debugging of PHP code (supplementary)

If you complete a file with 1200 lines of PHP code and view it in a browser, the error showing your code appears on line 561 of the file. At this point you have an easy way to find the line, follow these steps:

——Create a new notepad
——Copy your PHP code into it
——"Edit"->"Go to"
——Enter "561" and press Enter
——Your mouse is stuck on line 561
-- Look near that line to see if there are errors
——Fix the error, re-upload the code to your space, and it will most likely run normally. If there are still any errors, repeat the above steps.
Wandering Xiaosheng added: Nowadays, most people use software like editplus, this method is outdated

4. Use comments

If your PHP code has 1200 lines, it will be very difficult to figure out what it is going to do. The way to solve this problem is to add comments to your code.
PHP comments are different from the in HTML, because they will not be output (meaning they will not even be seen when "view source")
There are three ways to add comments in PHP:

PHP:

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

<&#63;php 
// 你的注释// 
# 你的注释
/*你的注释 */ 
&#63;>
Copy after login

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

You can decorate them however you wish, you are the only one using them.

5. Indentation of PHP code

I personally don’t like indenting PHP code, but it does make the code easier to read. When I have to indent, I do it using tabs, like this:

PHP:

--------------------------------------------------------------------------------
<&#63;php 
// Settings // 
  $var1 = "This";
// Showing Variables // 
  if($var1 == "This"){ 
    echo"You said This"; 
  }else{ 
    echo"You said That"; 
  } 
&#63;>
-------------------------------------------------------------------------------
Copy after login

6. Correct your PHP file inclusion method

I'm sure most of you here will need to include one or two other files within a file. Have you ever wondered what you would do if the file you need doesn't exist? Will people who browse your website think you are unprofessional?
In the PHP code I write, I always make sure that another file exists before including it, as in the following example:

PHP:

--------------------------------------------------------------------------------
<&#63;php 
if(!file_exists("layout.inc.php")){exit("Error : LayOut File Missing");}else{include_once("layout.inc.php");} 
&#63;>
-------------------------------------------------------------------------------- 
Copy after login

7. Database query

Sometimes your PHP code contains a connection to the database, and you may encounter some minor troubles. Most people who are prone to database problems write code in this form:

PHP:

--------------------------------------------------------------------------------
<&#63;php 
mysql_query("INSERT INTO tableName ('id','name') VALUES('1','Mike')"); 
&#63;>
-------------------------------------------------------------------------------- 
Copy after login

. . After running it, he found that the data was not inserted into the database. We can solve this problem like this:

PHP:

--------------------------------------------------------------------------------
<&#63;php 
mysql_query("INSERT INTO tableName ('id','name') VALUES('1','Mike')") or exit("MySQL Error : " . mysql_error()); 
&#63;>
-------------------------------------------------------------------------------- 
Copy after login

8. Abbreviate statements similar to IF-THEN

If you receive data from a registration page, and you want to ensure that all information is filled in, you might use statements that contain a lot of IF-THEN format, like this one:

PHP:

--------------------------------------------------------------------------------
<&#63;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.");} 
&#63;>
-------------------------------------------------------------------------------- 
Copy after login

And you can actually make it only one line by merging the IF-THEN statements of these two lines:

PHP:

--------------------------------------------------------------------------------
<&#63;php 
if((!$_POST[name]) || (!$_POST[email])){exit("Sorry, but you did not fill-in all of the requested fields.");} 
&#63;>
-------------------------------------------------------------------------------- 
Copy after login

|| and OR, && and AND have the same meaning respectively

9. Use echo or print?

Most people will say "echo and print are the same", and I agree with this point of view. However, echo runs much faster than print, and has one less letter than print. The echo command appeared later than print (I think so), so obviously you know what to choose.

10. Enter a large section of HTML from time to time

I believe many people have solutions to this problem, but I still want to tell you some solutions to this problem.
⑴. Enter the closing tag of PHP, and then you can enter the HTML code at will, and then the opening tag of PHP (I don’t like to do this because it looks very unprofessional).
⑵. Add a backslash to every sentence of HTML code (this is possible, but you have to do it all the time - every sentence).
⑶. Use the echo or print command, nothing more (recommended):

PHP:

--------------------------------------------------------------------------------
<&#63;php 
// Showing a huge chunk of HTML at a time // 
echo<<<END 
<font face="Verdana" color="Orange" size="3">Large, Orange Text in Font Size 3</font> 
<br><br> 
More HTML down here.. 
<br><br> 
<div align="Center">Centered text</div> 
END; 
&#63;>
--------------------------------------------------------------------------------
Copy after login

Actually, I have a lot of other things to say about modifying PHP code, but that’s it, I don’t want to bother you anymore.
Hope it helps everyone.

The above content is the entire description of how to improve programming efficiency and improve PHP programming technology introduced by the editor. I hope you like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1069351.htmlTechArticleHow does PHP programming development improve programming efficiency and improve PHP programming technology? Programming development programming technology uses single quotes instead of double quotes to include String, this is faster. Because PHP will work on both...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!