Ways to improve PHP programming techniques

Source: Internet
Author: User

Ways to improve PHP programming techniques

Here are a few ways to improve your PHP programming techniques.

1. PHP tags

I know some people who write PHP code like to use the <, but this is not a good habit, because the thumbnail tag on some servers can not be recognized correctly, and the use of standard PHP tags allows you to accurately compile your PHP code on any server.

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. 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.

There are three ways to add comments to PHP: PHP Programmer Station

The following is the referenced content:

<?php

Your comments//

# Your notes

/* Your comment */

?>

4, the PHP code indentation

The following is the referenced content:

<?php

Settings//

$var 1 = "this";

Showing Variables//

if ($var 1 = = "This") {

echo "You said this";

}else{

echo "you said that";

Www.phperz.com

}

?>

5, Fix your PHP file contains the way

PHP code that contains another file before it is determined to exist, as in the following example: http://www.pprar.com

The following is the referenced content:

<?php

if (!file_exists ("layout.inc.php")) {exit ("Error:layout file Missing");}

Else{include_once ("layout.inc.php");}

?>

6. 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:

The following is the referenced content:

<?php

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

?>

After running he found that the data is not inserted into the database, we can solve this problem: PHP Programmer station

The following is the referenced content:

<?php

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

or exit ("MySQL Error:".) Mysql_error ());

?>

7, 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:

The following is the referenced content:

<?php

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:

The following is the referenced content:

<?php

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.

8. 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.

9. 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 command, that's all (recommended): PHP source code Download down.phperz.com

The following is the referenced content:

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

?>

Hope that through the above content of the introduction, can bring you help.

Ways to improve PHP programming techniques

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.