您的当前位置:首页谈php+mysql扎实个人基本功_MySQL

谈php+mysql扎实个人基本功_MySQL

2020-11-09 来源:乌哈旅游


可以写成:
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
"Please upload the file of $type form.";
exit();
}
}


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

$ext_name
= strtolower(str_replace(".", "", strrchr($upfilename, ".")));
if (!(
$type==='') && strpos($type, $ext_name)===false)
{
echo
"Please upload the file of $type form.";
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 语句。
字段和表名用"`"引起来,避免保留字的影响。
如果看到下面这样的一个sql query,会让人比较头痛:
PHP代码:

$query
="select `flash_comment`.`content` , `flash_comment`.`nickname` , `flash_comment`.`date` , `flash_comment`.`ip` , `product`.`p_name` , `sgflash`.`fid` from `flash_comment` left join `product` on ( `flash_comment`.`p_no` = `product`.`p_no` ) left join `sgflash` on ( `product`.`p_name` = `sgflash`.`f_name` ) where `flash_comment`.`p_no` != '' order by `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`
LEFT JOIN `product` ON ( `flash_comment`.`p_no` = `product`.`p_no` )
LEFT JOIN `sgflash` ON ( `product`.`p_name` = `sgflash`.`f_name` )
WHERE `flash_comment`.`p_no` != ''
ORDER BY `flash_comment`.`date`"
;

//

显示全文