Phper/Pythoner技术联盟群:13734312.
1、函数的返回值不要为空值
| Both empty return and return with value used in functionL() |
|---|
| Category: Bug |
When a function sometimes returns a value and sometimes doesn'treturn a value (i.e., uses an emptyreturn; statement), itmay imply that the function may not behave properly in certaincircumstances. Alternatively, it may mean that the return
value(s)is/are redundant.
Exampleif ($a) { 改为 if($a){ |
2、分割字符串
explode — 使用一个字符串分割另一个字符串
array explode ( string$separator ,string$string [,int$limit ] )
例:
$pizza
$pieces
echo
echo
foreach ($pieces as $a){
}
3、格式化输出数组
用print_r打印数组,不是太友好。用var_dump函数打印数组非常到直观。
4、关于Get方法
HTTP GET 方法不适合大型的变量值;值是不能超过 100 个字符的。
5、$_REQUEST 变量
PHP 的 $_REQUEST 变量包含了 $_GET, $_POST 以及 $_COOKIE 的内容。
PHP 的 $_REQUEST 变量可用来取得通过 GET 和 POST 方法发送的表单数据的结果。
6、mktime()
mktime() 函数可为指定的日期返回 Unix 时间戳。
语法
mktime(hour,minute,second,month,day,year,is_dst)
如:$to= mktime(0,0,0,date("m"),date("d")+1,date("Y"));
7、不同浏览器,上传图片文件时,php端接收到的后缀名不同
对于 IE,识别 jpg 文件的类型必须是 pjpeg,对于 FireFox,必须是 jpeg。
png文件 ie为x-png,FireFox为png
8、处理cookie
设置cookie :
a、setcookie("TestCookie", $value, time()+3600); //经过 URL 编码
b、setrawcookie("TestCookie", $value, time()+3600);//不经过 URL 编码
使用cookie : $_COOKIE[] $_REQUESR[]
清除cookie:
setcookie("TestCookie", $value, time()-3600);//设置日期为过去的时间点。
9、PHP数据过滤器函数
filter_var() - 通过一个指定的过滤器来过滤单一的变量
filter_var_array() - 通过相同的或不同的过滤器来过滤多个变量
filter_input - 获取一个输入变量,并对它进行过滤
filter_input_array - 获取多个输入变量,并通过相同的或不同的过滤器对它们进行过滤
10、PHP 错误处理
基本的错误处理:使用 die() 函数
自定义错误处理函数:set_error_handler()
如:
<?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr";
}
//set error handler
set_error_handler("customError");
//trigger error
echo($test);
?>触发错误的函数:trigger_error()
11、异常处理
try
{
//------------
}
//捕获异常
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
set_exception_handler() 函数可设置处理所有未捕获异常的用户定义函数。
<?php
function myException($exception)
{
echo "<b>Exception:</b> " , $exception->getMessage();
}
set_exception_handler('myException');
throw new Exception('Uncaught Exception occurred');
?>12、字符串格式化sprintf()如 sprintf('%s %d','ddd',1);
1666

被折叠的 条评论
为什么被折叠?



