How to improve your PHP programming technology _php

Source: Internet
Author: User
Keywords technology programming improving How code PHP comments PHP files
Tags echo command
Hello everyone

I have decided to give you some caveats to improve your PHP code performance:

1. PHP tags

I know that some people like to use the thumbnail tag when they write PHP code- But this is not a good habit, because the thumbnail tag is not recognized correctly on some servers, and the use of standard PHP tags allows you to compile your PHP code exactly on any server. Maybe someday you'll need to install your code on a server that doesn't support thumbnail tags, so you'll have to spend one hours or more to sit down and upgrade your PHP code.

2, the debugging of PHP code

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

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

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 that displays your code appears on 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 in.
--"edit", "Go to"
--Input "561", enter
--your mouse stays on line 561.
--See if there is an error near the line
--Fix the bug, re-upload the code into your space, and it will probably run normally. If there are any errors, repeat the above steps.

Wandering a niche add: now most use EditPlus and the like software, this method is outdated OH

4. Use annotations

If you have 1200 lines of PHP code, it is difficult to figure out what it is going to do, and the solution to this problem is to annotate your code.
PHP comments are different from HTML because it will not be output (because they are not even seen in "viewing source files")
There are three ways to add comments to PHP:

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

Your comments//
# Your notes
/* Your comment */
?>

--------------------------------------------------------------------------------
You can decorate them according to what you want, and you are the only one who uses them.

5, the PHP code indentation

I personally don't really like indenting PHP code, but it does make the code easy to read. When I have to indent, I use tab to do the following:

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

Settings//
$var 1 = "this";

Showing Variables//
if ($var 1 = = "This") {
echo "You said this";
}else{
echo "you said that";
}
?>

--------------------------------------------------------------------------------
6, Fix your PHP file contains the way

I'm sure most of the people here will need to include one or two other files in a file. Ever wonder what to do if the file you need is not there? Does the person who browses your website feel that you are not professional?
In the PHP code I've written, I'll make sure it exists before I include another file, as 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 a connection to the database, you may encounter a little trouble, most people prone to database problems are in this form to write code:

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

mysql_query ("INSERT into tableName (' id ', ' name ') VALUES (' 1 ', ' Mike ')");
?>

--------------------------------------------------------------------------------
。。 After running, he found that the data was not inserted into the database, and we can solve this problem:

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

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

--------------------------------------------------------------------------------
8, abbreviations similar to if-then statements

If you receive data from a registration page and you want to make sure that all the information is filled in, you might use a statement that contains many if-then formats, like this:

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

if (!$_post[name]) {exit ("Sorry, but do not have fill-in all of the requested fields.");}
if (!$_post[email]) {exit ("Sorry, but do not have fill-in all of the requested fields.");}
?>

--------------------------------------------------------------------------------
Instead, you can actually merge the two lines by merging the If-then statement to make it only one line:

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

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

--------------------------------------------------------------------------------
|| and or,&& and and each have the same meaning.

9. Use Echo or print?

Most people will say "Echo and print are the same", and I agree with this view. 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 how to choose.

10. Enter a large section of HTML language

I believe a lot of people have a solution to this, but I still want to talk to you about some ways to solve this.
⑴, enter the end tag of the PHP, then you can enter the HTML code at will, then the PHP start tag (I do not like this, because it looks very unprofessional).
⑵, add backslashes in every HTML code (this is possible, but you always have to--every sentence).
⑶, using the Echo or print commands, that's all (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;
?>

--------------------------------------------------------------------------------
I actually have a lot of other things to say about modifying PHP code, but that's it, I don't want to keep bothering you.
We 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.