Archive for January, 2008
count使用要注意的
翻手册时看到的,贴出来备忘 mysql> SELECT COUNT(*) FROM student; 这个优化仅适用于 MyISAM表, 原因是这些表类型会储存一个函数返回记录的精确数量,而且非常容易访问。对于事务型的存储引擎(InnoDB, BDB), 存储一个精确行数的问题比较多,原因是可能会发生多重事物处理, 而每个都可能会对行数产生影响。
可以给assign单独传一个array作为参数
一段smarty源代码,可以给assign单独传一个array作为参数 /** * assigns values to template variables * * @param array|string $tpl_var the template variable name(s) * @param mixed $value the value to assign */ function assign($tpl_var, $value = null) { if (is_array($tpl_var)){ foreach ($tpl_var as $key => $val) { if ($key != ”) { $this->_tpl_vars[$key] = $val; } } } else { if ($tpl_var != ”) $this->_tpl_vars[$tpl_var] = $value; } }
组织 WHERE子句的小技巧(OR)
有时需要传入多个condition(至少一个), 可以这样子来写 $where = “”; foreach($allCondition AS $condition){ if($where != “”){ $where .= ” OR “; } $where = “`conditionX`=” . addslashes($condition); } 这样最后生成的 WHERE 子句就是”XXX OR XXX OR …”而当只有一个条件时为”XXX”
关于$a==1与1==$a的总结
开始作者认为有效率上的原因, 后来我将其代码倒置 发现$a=1反而更快,可见1==$a不是出于效率,而是为了避免逻辑错误。 另有人指出 == 与 === 在性能上存在明显的效率差异。
使用count()要注意的两点
一、null和false的不同 $result = count(null); // $result == 0 $result = count(false); // $result == 1 二、是否递归计算 <?php $food = array(‘fruits’ => array(‘orange’, ‘banana’, ‘apple’), ‘veggie’ => array(‘carrot’, ‘collard’,'pea’)); // recursive count echo count($food, COUNT_RECURSIVE); // output 8 // normal count echo count($food); // output 2 ?>