How to improve your PHP programming technology

Source: Internet
Author: User
Hello everyone, I decided to share with you some notes that can improve the PHP code performance: 1. PHP labels I know some people prefer to use the scaling label when writing PHP code -??, But this is not a good habit, because scaling labels cannot be correctly identified on some servers, and the use of standard PHP labels allows you to accurately compile on any server.

I decided to tell you some notes that can improve the PHP code performance:

1. PHP labels

I know some people prefer to use scaling labels when writing PHP code- But this is not a good habit, because scaling labels cannot be correctly identified on some servers, the use of standard PHP labels 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 scaling labels, so you will have to spend an hour or more time sitting down and upgrading your PHP code.

2. PHP code debugging

Sometimes we encounter problems when running PHP code, and we don't know where the problem is. In PHP, there is an error_reporting () function, which tells 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 in the second line of the file:

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

Error_reporting (E_ALL );

--------------------------------------------------------------------------------
3. PHP code debugging (supplement)

If you have completed a file with 1200 lines of PHP code and browsed it in a browser, the error of your code appears in the 561st line of the file. In this case, you have a simple method to find the row, follow these steps:
-- Create a notebook
-- Copy your PHP code
-- "Edit"-> "go"
-- Enter "561" and press enter.
-- Your mouse is stuck at 561 rows.
-- Check for errors near this row
-- Fixed the error and re-uploaded the code to your space, which is likely to run normally. If there are any errors, repeat the preceding steps.

Xiaoyi Xiaosheng added: Currently, most of the software such as editplus is used. this method is outdated.

4. Use annotations

If your PHP code contains 1200 lines, it is very difficult to understand what it is to do. The solution to this problem is to add comments to your code.
PHP annotations are different from those in HTML Because it will not be output (thought they will not even be seen when "viewing source files)
There are three methods to add comments in PHP:

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

// Your comment //
# Your comment
/* Your comment */
?>

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

5. PHP code indentation

I personally do not like to indent PHP code, but it does make the code easy to read. When I have to indent, I use a tab, as shown below:

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

// Settings //
$ Var1 = "This ";

// Showing Variables //
If ($ var1 = "This "){
Echo "You said This ";
} Else {
Echo "You said That ";
}
?>

--------------------------------------------------------------------------------
6. modify the php file inclusion method.

I'm sure most people here need to include one or two other files in one file. Have you ever wondered what to do if the file you need does not exist? Will the person who browsed your webpage feel that you are not professional enough?
In my PHP code, I will determine whether another file exists before it is included, as shown in the following example:

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

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 database connections, and you may encounter some minor troubles. most people who are prone to database problems write code in this form:

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

Mysql_query ("insert into tableName ('id', 'name') VALUES ('1', 'Mike ')");
?>

--------------------------------------------------------------------------------
.. After running, he finds that the data is not inserted into the database. we can solve this problem as follows:

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

Mysql_query ("insert into tableName ('id', 'name') VALUES ('1', 'Mike ')") or exit ("MySQL Error :". mysql_error ());
?>

--------------------------------------------------------------------------------
8. scale down statements similar to IF-THEN

IF you want to ensure that all the information has been filled in, you may use statements that contain many IF-THEN formats, as shown below:

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

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 .");}
?>

--------------------------------------------------------------------------------
In fact, you can merge the IF-THEN statements of the two rows to make it only have one row:

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

If ((! $ _ POST [name]) | (! $ _ POST [email]) {exit ("Sorry, but you did not fill-in all of the requested fields .");}
?>

--------------------------------------------------------------------------------
| And or, & AND have the same meaning.

9. using echo or print?

Most people will say "echo and print are the same". I agree with this. However, echo runs much faster than print and has one letter less than print. The echo command appears a little later than print (I think so). Obviously, you know how to choose it.

10. enter a large HTML language from time to time

I believe many people have a solution to this problem, but I still want to talk about some solutions.
(1) enter the end tag of PHP, and then enter the HTML code at will, and then the start tag of PHP (I don't like this because it looks unprofessional ).
(2) add a backslash to each HTML code (this is feasible, but you always have to do this-every sentence is required ).
(3) use the echo or print command. (recommended ):

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

// Showing a huge chunk of HTML at a time //
Echo < Large, Orange Text in Font Size 3



More HTML down here ..



Centered text


END;
?>

--------------------------------------------------------------------------------
In fact, I still have many other things to talk about modifying PHP code, but that's it. I don't want to bother you any more.
Hope to help you.

Best Regards,
-Mike.

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.