网普科技
网普管理员
![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) 网普科技人民公仆
积分 3080
发贴 2863
注册 2004-6-26 来自 网普科技
状态 离线
|
#1 转:How To : Improve Your PHP Programming
Hello everyone,
I've decided that I would make a thread here describing the different things that I do to improve other people's PHP scripts.
Hope you enjoy it and learn a thing or two.
1 - Your PHP Tags
I know some of you prefer to use the short tags when writing PHP scripts <? ?> but this is not always the best way of doing it.
The standard tags <?php ?> are much better as they will work on every server that you write your PHP code on. You may move to a server some day that doesn't allow the short tags or the ASP-style tags and you will have to sit for an hour and update your PHP scripts.
2 - Debugging Your PHP Code
Some of us may run into a problem when programming a PHP script and don't know what's wrong with it. The error_reporting() function in PHP helps you out by telling every error you have on your page. To show all of the errors on the page that you're editing, put this on the second line :CODE: [Copy to clipboard]
PHP:
--------------------------------------------------------------------------------
error_reporting(E_ALL);
-------------------------------------------------------------------------------- 3 - Debugging Your PHP Code (again)
When you finish editing your 1200-line PHP script, click onto it in your Internet browser, you see an error that says that is on line 561. Don't hit the panic-attack button quite yet, because there is an easy way to find out what line 561 is. Follow these easy steps :
- Open up Microsoft Notepad
- Paste your PHP script into it
- Go to 'Edit' >> 'Go To...' (or Control+G)
- Type in line #561 and hit the enter key
- Your cursor is taken to line #561.
- Look above and below line #561 to see if there is any kind of trouble.
- Fix the error, re-upload the script to your website, and most likely it will work. If there is another error, repeat the above steps.
4 - Using Comments
If you have a 1200-line PHP script, it may be quite hard to figure out what's going on all through-out it. The solution to figure out what you're doing is to add PHP-comments.
PHP-comments are different than the <!-- HTML Comments --> as they are not outputted to the user's page (meaning that they are not even going to see it in the source code).
There are three ways to make comments in PHP :CODE: [Copy to clipboard]
PHP:
--------------------------------------------------------------------------------
<?php
// The double-backslash is my personal favorite. I add another set after my code so that it looks even, though it is not necessary. //
# The hash-style comment is another way of making a comment.
/* And, this is the final way of making PHP-comments. You can use multiple
lines at a time by using this style. */
?>
-------------------------------------------------------------------------------- You can decorate it however you like, you are the only one who may use them.
5 - Indenting Your PHP Codes
I don't personally like to indent my PHP codes, but it helps when reading it. When I do have to, I use the tab key to accomplish this. Example :CODE: [Copy to clipboard]
PHP:
--------------------------------------------------------------------------------
<?php
// Settings //
$var1 = "This";
// Showing Variables //
if($var1 == "This"){
echo"You said This";
}else{
echo"You said That";
}
?>
-------------------------------------------------------------------------------- 6 - Improving your PHP-File Includes
I'm sure that most of us on here include a PHP file or two for our layouts. Well, what if your layout file was missing ? Wouldn't that look pretty unprofessional to the people on your website ?
In every PHP-script that I write, I make sure that the file exists before it is even included. Here's an example :CODE: [Copy to clipboard]
PHP:
--------------------------------------------------------------------------------
<?php
if(!file_exists("layout.inc.php")){exit("Error : LayOut File Missing");}else{include_once("layout.inc.php");}
?>
-------------------------------------------------------------------------------- I'm sure that a small error message will seem better than half a page that is all messed-up looking.
7 - Your MySQL Queries
Sometimes when you're writing a PHP script that includes connections to your MySQL database, you may run into a few problems. Most everyone that had MySQL problems ran a command like this one :CODE: [Copy to clipboard]
PHP:
--------------------------------------------------------------------------------
<?php
mysql_query("INSERT INTO tableName ('id','name') VALUES('1','Mike')");
?>
-------------------------------------------------------------------------------- ..and they figure out that it's not inserting into their database. Here's the solution to this :CODE: [Copy to clipboard]
PHP:
--------------------------------------------------------------------------------
<?php
mysql_query("INSERT INTO tableName ('id','name') VALUES('1','Mike')") or exit("MySQL Error : " . mysql_error());
?>
-------------------------------------------------------------------------------- 8 - Combining Alike If-Then Statements
You may have a register page, and want to make sure that everything has been filled-in. You may use many if-then statements like so :CODE: [Copy to clipboard]
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.");}
?>
-------------------------------------------------------------------------------- You can combine these two lines into one by joining their if-then statements together :CODE: [Copy to clipboard]
PHP:
--------------------------------------------------------------------------------
<?php
if((!$_POST[name]) || (!$_POST[email])){exit("Sorry, but you did not fill-in all of the requested fields.");}
?>
-------------------------------------------------------------------------------- Simply, || is the same thing as OR and && is the same as AND.
9 - Using echo or print ?
Most of you may say 'echo is the same thing as print', in which I agree with you all. The echo command is much faster than the print command, and is one less character to type. The echo command came later than the print command (I believe), so you make the judement on which to use.
10 - Printing out a Huge Chunk of HTML at a Time
Well, I'm sure that many of us found a way to get around this, but I'd like to share with you a few of the ways you can do it.
1 - Break off your PHP-code, print the HTML, and start your PHP-code up again. (I don't prefer doing this as it looks pretty unprofessional to me).
2 - Adding backslashes to each HTML tag. (It works, but takes forever to do).
3 - Using the echo/print command, but without having to do much work. (I recommend) :CODE: [Copy to clipboard]
PHP:
--------------------------------------------------------------------------------
<?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;
?>
-------------------------------------------------------------------------------- Well, I have many other things to tell about sprucing up your PHP-code, but I don't want to bore you.
I hope I've helped.
Best Regards,
- Mike.
|
![](images/common/sigline.gif)
天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。
网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818
发送QQ消息 |
|