发布时间:2021-07-07 09:54:01 阅读次数:152
考虑过使用 ob_gzhandler 吗? 不要那样做. 毫无意义. php只应用来编写应用. 不应操心服务器和浏览器的数据传输优化问题.
使用apache的mod_gzip/mod_deflate 模块压缩内容.
时常会用php输出动态javascript内容:
$images = array( 'myself.png' , 'friends.png' , 'colleagues.png'); $js_code = '';foreach($images as $image) { $js_code .= "'$image' ,"; } $js_code = 'var images = [' . $js_code . ']; '; echo $js_code;//Output is var images = ['myself.png' ,'friends.png' ,'colleagues.png' ,];
更聪明的做法, 使用 json_encode:
$images = array( 'myself.png' , 'friends.png' , 'colleagues.png'); $js_code = 'var images = ' . json_encode($images); echo $js_code;//Output is : var images = ["myself.png","friends.png","colleagues.png"]
优雅乎?
写或保存文件前, 确保目录是可写的, 假如不可写, 输出错误信息. 这会节约你很多调试时间. linux系统中, 需要处理权限, 目录权限不当会导致很多很多的问题, 文件也有可能无法读取等等.
确保你的应用足够智能, 输出某些重要信息.
$contents = "All the content"; $file_path = "/var/www/project/content.txt"; file_put_contents($file_path , $contents);
这大体上正确. 但有些间接的问题. file_put_contents 可能会由于几个原因失败:
>>父目录不存在
>>目录存在, 但不可写
>>文件被写锁住?
所以写文件前做明确的检查更好.
$contents = "All the content"; $dir = '/var/www/project'; $file_path = $dir . "/content.txt";if(is_writable($dir)) { file_put_contents($file_path , $contents); }else{ die("Directory $dir is not writable, or does not exist. Please check"); }
这么做后, 你会得到一个文件在何处写及为什么失败的明确信息.
在linux环境中, 权限问题可能会浪费你很多时间. 从今往后, 无论何时, 当你创建一些文件后, 确保使用chmod设置正确权限. 否则的话, 可能文件先是由”php”用户创建, 但你用其它的用户登录工作, 系统將会拒绝访问或打开文件, 你不得不奋力获取root权限, 更改文件的权限等等.
// Read and write for owner, read for everybody elsechmod("/somedir/somefile", 0644);// Everything for owner, read and execute for otherschmod("/somedir/somefile", 0755);
if($_POST['submit'] == 'Save') { //Save the things}
上面大多数情况正确, 除了应用是多语言的. ‘Save’ 可能代表其它含义. 你怎么区分它们呢. 因此, 不要依赖于submit按钮的值.
if($_POST['submit'] == 'Save') { //Save the things}
现在你从submit按钮值中解脱出来了.
//Delay for some timefunction delay() { $sync_delay = get_option('sync_delay'); echo "Delaying for $sync_delay seconds..."; sleep($sync_delay); echo "Done "; }
用静态变量取代:
//Delay for some timefunction delay() { static $sync_delay = null; if($sync_delay == null) { $sync_delay = get_option('sync_delay'); } echo "Delaying for $sync_delay seconds..."; sleep($sync_delay); echo "Done "; }
某些简单例子:
$_SESSION['username'] = $username; $username = $_SESSION['username'];
这会导致某些问题. 如果在同个域名中运行了多个应用, session 变量可能会冲突. 两个不同的应用可能使用同一个session key. 例如, 一个前端门户, 和一个后台管理系统使用同一域名.
从现在开始, 使用应用相关的key和一个包装函数:
define('APP_ID' , 'abc_corp_ecommerce');//Function to get a session variablefunction session_get($key) { $k = APP_ID . '.' . $key; if(isset($_SESSION[$k])) { return $_SESSION[$k]; } return false; }//Function set the session variablefunction session_set($key , $value) { $k = APP_ID . '.' . $key; $_SESSION[$k] = $value;
假如你在某文件中定义了很多工具函数:
function utility_a() { //This function does a utility thing like string processing} function utility_b() { //This function does nother utility thing like database processing} function utility_c() { //This function is ...}
这些函数的使用分散到应用各处. 你可能想將他们封装到某个类中:
class Utility { public static function utility_a() { } public static function utility_b() { } public static function utility_c() { } }//and call them as
显而易见的好处是, 如果php内建有同名的函数, 这样可以避免冲突.
另一种看法是, 你可以在同个应用中为同个类维护多个版本, 而不导致冲突. 这是封装的基本好处, 无它.
>>使用echo取代print
>>使用str_replace取代preg_replace, 除非你绝对需要
>>不要使用 short tag
>>简单字符串用单引号取代双引号
>>head重定向后记得使用exit
>>不要在循环中调用函数
>>isset比strlen快
>>始中如一的格式化代码
>>不要删除循环或者if-else的括号
不要这样写代码:
if($a == true) $a_count++;
这绝对WASTE.
写成:
if($a == true) { $a_count++; }
不要尝试省略一些语法来缩短代码. 而是让你的逻辑简短.
>>使用有高亮语法显示的文本编辑器. 高亮语法能让你减少错误.
比如说你想 trim 数组中的所有元素. 新手可能会:
foreach($arr as $c => $v) { $arr[$c] = trim($v); }
但使用 array_map 更简单:
foreach($arr as $c => $v) { $arr[$c] = trim($v); }
这会为$arr数组的每个元素都申请调用trim. 另一个类似的函数是 array_walk.