当前位置: 首页 > 图文教程 > 网络编程 > PHP > php+mysql扎实个人基本功

PHP
MYSQL版本大于4.1问题 - PHPchina
怎么让用户点击一个连接后,把一个图片另存了 - PHPchina
武汉10月15日Phper聚会召集!!! - PHPchina
php如果不等待exec执行的程序创建的子进程? - PHPchina
哪位知道DISCUZ处理防SQL注入的代码是哪部分 - PHPchina
求教!我实在不知道哪里问题,在线等ing - PHPchina
怎样结束用户某一进程 - PHPchina
比对用户名密码能不能这样写? - PHPchina
求助:如何在PHP+mysql中实现数据备份? - PHPchina
大家看看这个配置对吗 - PHPchina
如何禁止require当前文件 - PHPchina
无法将回调函数放在类中? - PHPchina
村里 PHP代码高亮是怎么实现的? - PHPchina
apache安装后.服务里没有apache2这个服务! - PHPchina
请教一个小问题 - PHPchina
config.php里面是不是应该把多数参数设置为常量而不是变量? - PHPchina
请教高手一个问题 - PHPchina
如何让百度收录我的网站 ?? - PHPchina
谁能给个注入的简单语句? - PHPchina
求PHP站内搜索思路 - PHPchina

PHP 中的 php+mysql扎实个人基本功


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 126 ::
收藏到网摘: n/a

一.10句话
1.不要依赖register_global=ON的环境,从你刚懂得配置php运行环境甚至尚不明白register_global的ON/OFF会对自己有什么影响的那天起,就应该勇敢地把它设为OFF.
2.写程序前看看怎么用error_reporting.
3.不懂就问本身没错,但你需要在那之前查查手册。
4.当然,你需要懂得使用手册。手册上找不到答案的时候,应该考虑下网络上的搜索引擎。
5.刚学会php+mysql之后,不要叫嚷着要写论坛,要写XXX。要明白,刚学会写汉字并不表示你有能力写诗。
6.在学web编程的时候,你应该先去认识html这个朋友。
7.有点能力后,试着回答新手的问题,不要看到自己懂的而别人不懂就沾沾自喜,扔下一名“简单,那是基本的东西”就走更要不得。
8.思考是一个好习惯,不动手去写就等于空想,什么也没有。
9.写好一段程序,如果觉得很满意,一周后再看一遍,也许你会认为它应该有所改变
10.有空多看看别人的程序,找出他人的不足或优点,自己掂量。

二.各取所需

1.善于使用“引用”,它能直接影响到程序的效率。

2.善于用三元运算子,可以让程式较精简有效率。
比如:

PHP代码:

if($data[$i]['nickname'])
{
$nickname=$data[$i]['nickname'];
}
else
{
$nickname=$data[$i]['ip'];
}


可以写成:

PHP代码:

$nickname=$data[$i]['nickname']?$data[$i]['nickname']:$data[$i]['ip'];



3.善于组织if...else...回圈
比如:

PHP代码:

$ext_name=strtolower(str_replace(".","",strrchr($upfilename,".")));
if(!empty($type))
{
if(!strpos($type,$ext_name))
{
echo"Pleaseuploadthefileof$typeform.";
exit();
}
}


上面的代码你应该写成这样:

PHP代码:

$ext_name=strtolower(str_replace(".","",strrchr($upfilename,".")));
if(!($type==='')&&strpos($type,$ext_name)===false)
{
echo"Pleaseuploadthefileof$typeform.";
exit();
}



4.尽量让你的代码清淅些
如果写成这样,是比较让人头痛的:

PHP代码:

$foo=$_post["foo"];
$username=$_post["user"];
$group=$_POST["group"];
if($group=="wheel"){
$username=$username."wheel";
}


同样的代码,这样就比较让人看得舒服了:

PHP代码:

$foo=$_post["foo"];
$username=$_post["username"];
$group=$_POST["group"];
if($group=="wheel")
{
$username=$username."wheel";
}


当然,有一定基础后,你应该要写成这样:

PHP代码:

$foo=&$_POST['foo'];
$username=$_POST["group"]!='wheel'?$_POST["username"]:$_POST["username"].'wheel';


5.编写规范的mysql语句。
字段和表名用"`"引起来,避免保留字的影响。
如果看到下面这样的一个sqlquery,会让人比较头痛:

PHP代码:

$query="select`flash_comment`.`content`,`flash_comment`.`nickname`,`flash_comment`.`date`,`flash_comment`.`ip`,`product`.`p_name`,`sgflash`.`fid`from`flash_comment`leftjoin`product`on(`flash_comment`.`p_no`=`product`.`p_no`)leftjoin`sgflash`on(`product`.`p_name`=`sgflash`.`f_name`)where`flash_comment`.`p_no`!=''orderby`flash_comment`.`date`";


同样的一个query,写成这样就令人看得明白得多了:

PHP代码:

$query="SELECT`flash_comment`.`content`,`flash_comment`.`nickname`,`flash_comment`.`date`,`flash_comment`.`ip`,`product`.`p_name`,`sgflash`.`fid`
FROM`flash_comment`
LEFTJOIN`product`ON(`flash_comment`.`p_no`=`product`.`p_no`)
LEFTJOIN`sgflash`ON(`product`.`p_name`=`sgflash`.`f_name`)
WHERE`flash_comment`.`p_no`!=''
ORDERBY`flash_comment`.`date`";