<?php $a = '0'; var_dump(empty($a)); //true
<?php $a = array(); var_dump(empty($a));//true $a = array(array()); var_dump(empty($a));//false
<?php $count = 5; function get_count(){ static $count = 0; return $count++; } echo $count;//5 ++$count; echo get_count();//0 echo $count;//6 echo get_count();//1 由此可见,static是将变量保存到函数的作用域内,功能类似于闭包
<?php $GLOBALS['var1'] = 5; $var2 = 1; function get_value(){ global $var2; $var1 = 0; return $var2++; } get_value(); echo $var1; //5 echo $var2; //2
使用五种以上方式获取一个文件的扩展名 function get_ext1($file_name){ return strrchr($file_name, '.'); } function get_ext2($file_name){ return substr($file_name, strrpos($file_name, '.')); } function get_ext3($file_name){ return array_pop(explode('.', $file_name)); } function get_ext4($file_name){ $p = pathinfo($file_name); return $p['extension']; } function get_ext5($file_name){ return strrev(substr(strrev($file_name), 0, strpos(strrev($file_name), '.'))); }
<?php //快速排序(数组排序) function quick_sort($array) { if (count($array) <= 1) return $array; $key = $array[0]; $left_arr = array(); $right_arr = array(); for ($i=1; $i<count($array); $i++){ if ($array[$i] <= $key) $left_arr[] = $array[$i]; else $right_arr[] = $array[$i]; } $left_arr = quick_sort($left_arr); $right_arr = quick_sort($right_arr); return array_merge($left_arr, array($key), $right_arr); }