This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func fibonacci() func() int { | |
| a, b := -1, 1 | |
| return func() int { | |
| a, b = b, a+b | |
| return b | |
| } | |
| } | |
| func main() { | |
| f := fibonacci() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //被观察者接口 | |
| interface ObservableInterface { | |
| public function attach(ObserverInterface $observer); | |
| public function detach(ObserverInterface $observer); | |
| public function notify(); | |
| } | |
| //被观察者 (订单、用户登录、注册) | |
| // 这个Observable可以换成Order来理解 | |
| class Observable implements ObservableInterface { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * 根据两点间的经纬度计算距离 | |
| * @param $lng1 | |
| * @param $lat1 | |
| * @param $lng2 | |
| * @param $lat2 | |
| * @return int | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $nums = 2; //按开关的次数 | |
| for ($i = 1; $i <= 100; $i++) { | |
| $arr[$i] = 0; | |
| } | |
| //这个数组生成可以使用下面的自带函数 | |
| //$arr=array_fill(1,100,0); //填充100盏灯都是关闭状态 | |
| $count = count($arr); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * PHP数组不定长多键值排序 | |
| * | |
| * @author Zjmainstay | |
| * @website http://www.zjmainstay.cn, https://glot.io/snippets/ernfkfnjhb | |
| * @param array $list 数据源 | |
| * @param array $rules 排序规则 ['key1'=>'asc', 'key2' => 'desc', ...] | |
| * @return array | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PHP写日志文件需要打开、写入和关闭文件等操作,PHP有fopen(),fwrite()和fclose()三个函数与之对应,而另一个函数file_put_contents()它也能字符串写入文件,其实这个函数实现了依次调用 fopen(),fwrite() 以及 fclose()。所以我们使用file_put_contents()非常简洁。值得注意的是,往文件后面追加内容时需要带上参数:FILE_APPEND。 | |
| 实际运行中,我们有可能会遇到日志文件超大的情况,所以我们设置一个最大值,当日志文件大小超过这个最大值时,将此日志文件备份好,然后重新生成一个新的日志文件来记录新的日志内容。 | |
| 在写日志前,我们将日志内容进行json格式化,所以需要将内容转化成JSON格式,然后写入文件。当然你也可以不用json,或者换作别的工具程序(如日志分析工具)可以阅读的格式。总之,我们写入的内容是方便必要时可以方便读取。 | |
| <?php | |
| /* | |
| * 日志类 | |
| * 每天生成一个日志文件,当文件超过指定大小则备份日志文件并重新生成新的日志文件 | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * glob递归读取某个目录下的文件 | |
| */ | |
| function glob_recursive($pattern, $flags = 0) | |
| { | |
| $files = glob($pattern, $flags); | |
| foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| foreach( glob( ngx_log. "/*.log" ) as $file ) { | |
| $log = new SplFileObject($file); | |
| foreach( $log as $line ){ | |
| $ipnum = getip($line); | |
| if( $ipnum ) { | |
| $ips[ $ipnum ] = (int)$ips[ $ipnum ] + 1; | |
| //这里可以做一些数据入库处理 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * 通过出生日期获取具体年龄 | |
| * @param type date | |
| * @return type number | |
| */ | |
| function get_age($birthday) | |
| { | |
| $age = date('Y', time()) - date('Y', strtotime($birthday)) - 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 此方法由@Tonton 提供 | |
| * http://my.oschina.net/u/918697 | |
| * @date 2012-12-12 | |
| */ | |
| function genTree5($items) { | |
| foreach ($items as $item) | |
| $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]; | |
| return isset($items[0]['son']) ? $items[0]['son'] : array(); | |
| } |