PHP常见面试题汇总(二)
- //第51题:统计一维数组中所有值出现的次数?返回一个数组,其元素的键名是原数组的值;键值是该值在原数组中出现的次数
- $array=array(4,5,1,2,3,1,2,"a","a");
- $ac=array_count_values($array);
- /**
- *输出结果:
- *Array(
- *[4]=>1
- *[5]=>1
- *[1]=>2
- *[2]=>2
- *[3]=>1
- *[a]=>2
- *)
- */
- echo"<pre>";print_r($ac);
[php]view plaincopy
- //第52题:如何统计字符串中每种字符的出现次数并排序?
- functioncountStr($str){
- $str_array=str_split($str);//str_split()函数能将字符串中每个字符都转化为数组的元素
- $str_array=array_count_values($str_array);
- arsort($str_array);//arsort()函数对数组进行逆向排序并保持索引关系
- return$str_array;
- }
- $str="asdfgfdas323344##$\$fdsdfg*$**$*$**$$443563536254fas";
- /**
- *输出结果:
- *Array(
- [$]=>7
- [3]=>6
- [*]=>6
- [4]=>5
- [f]=>5
- [s]=>4
- [d]=>4
- [5]=>3
- [a]=>3
- [6]=>2
- [2]=>2
- [g]=>2
- [#]=>2
- )
- */
- echo"<pre>";print_r(countStr($str));
[php]view plaincopy
- //第53题:str_word_count()函数计算字符串中的单词数?
- /**
- *输出结果:2
- */
- echostr_word_count("Helloworld!");//参数二:默认0,返回单词的数目
- /**
- *输出结果:
- Array(
- [0]=>Hello
- [1]=>world
- )
- */
- echo"<pre>";print_r(str_word_count("Helloworld!",1));//参数二:1-返回包含字符串中的单词的数组
- /**
- *输出结果:
- Array(
- [0]=>Hello
- [6]=>world
- )
- */
- echo"<pre>";print_r(str_word_count("Helloworld!",2));//参数二:2-返回一个数组,其中的键是单词在字符串中的位置,值是实际的单词.
[php]view plaincopy
- <?php
- /**
- *第56题:redis与memcached区别?
- *
- *不同点:
- *
- *(1)redis中并不是所有数据在有效期内只能常驻内存的(如果需要,可定期同步持久化到磁盘),这是和memcached相比一个最大的区别(memcached中的数据在有效期内是以键值对的形式常驻内存的)
- *(2)redis不仅仅支持简单的键值对类型的数据,同时还提供list,set,hash等数据结构的存储;memcached仅支持简单的键值对类型的数据,但是memcached却可以缓存图片、视频等等数据
- *(3)redis支持数据的备份,即master-slave模式的数据备份
- *(4)redis支持数据的持久化和数据同步,可以将内存中的数据保存在磁盘中,重启系统后可以再次加载进行使用,缓存数据不会因此而丢失.memached缓存数据是常驻内存的,重启系统后数据就没了
- *(5)redis可以通过expire设定有效期,memcached在set数据的时候可以指定要缓存的数据永不过期
- *(6)redis可以做一主多从;memcached也可以做一主多从
- *(7)redis当物理内存用完时,可以将一些很久没用到的value交换到磁盘;memcached当物理内存用完后就会自动清理一些早期的数据
- *
- *相同点:
- *
- *(1)redis和memcached都是将数据存放在内存中,都是内存数据库
- *(2)redis和memcached都可以做一主多从
- *
- *性能:
- *
- *(1)redis根据其官方的测试结果:在50个并发的情况下请求10w次,写的速度是110000次/s,读的速度是81000次/s
- *(2)redis将键名与值的最大上限各自设定为512MB;memcached则<span></span>将键名限制在250字节,值也被限制在不超过1MB,且只适用于普通字符串.
- *
- *何时使用memcached:
- *
- *(1)小型静态数据:当我们需要缓存小型静态数据的时候可以考虑memcached,最具代表性的例子就是HTML代码片段;因为memcached的内部内存管理机制虽然不像redis的那样复杂,但却更具实际效率,这是因为memcached在处理元数据时所消耗的内存资源相对更少.作为memcached所支持的惟一一种数据类型,字符串非常适合用于保存那些只需要进行读取操作的数据,因为字符串本身无需进行进一步处理<span></span>.
- *
- */
- ?>
[php]view plaincopy
- <?php
- //第57题:memcached的使用案例
- //第一步:获取数据
- functionget_data(){
- $sql="SELECT*fromorder_master";//一般来说都是超级复杂的sql语句
- return$this->db->cache_all($sql);//获取缓存数据(如果缓存数据过期,则重新查询数据库并将查询到的数据放到memcache中并返回)
- }
- //第二步:获取memcache中缓存数据
- publicfunctioncache_all($sql,$reload=false,$limit=null){//参数1-sql语句、参数2-是否从数据库中重新获取数据并写入memcache、参数3-每次获取记录数量
- $this->reload=$reload;
- $sql=$this->get_query_sql($sql,$limit);//拼接sql语句,如:‘select*fromorder_masterlimit0,10‘
- return$this->get_cache($sql,‘get_all‘);//参数1-拼接的sql、参数2-要执行的方法名;将sql语句作为参数调用get_all()方法
- }
- //第三步:从memcache中获取缓存数据提示:1、如果缓存数据过期,则重新查询数据库并将查询结果放到memcache缓存中,返回数据.2、如果缓存数据未过期,则直接获取缓存数据
- protectedfunctionget_cache($sql,$method){
- $cache_file=md5($sql.$method);//memcache中缓存数据的键名,格式:键名=md5(sql语句+方法名)提示:一定要保证键名唯一
- $res=$this->cache->get($cache_file);//根据缓存数据的键名获取键值
- if(!$res){//memcache中缓存数据过期的情况//如果memcache中缓存数据过期,则重新从数据库中获取数据,并将结果以键值对形式存储到memcache中
- $res=$this->$method($sql);//调用$this->get_all($sql);//直接从数据库中获取数据并返回
- if($res&&$this->cache_mark&&!$this->reload){
- $this->cache->set($cache_file,$res);//将数据库中查询到的数据以键值对形式set到memcache缓存中
- }
- }
- return$res;//返回数据
- }
- //第四步:直接从数据库中获取数据并返回
- publicfunctionget_all($sql,$limit=null,$fetch_mode=MYSQLI_ASSOC){
- $this->query($sql,$limit);
- $all_rows=array();
- $this->fetch_mode=$fetch_mode;
- while($rows=$this->fetch()){
- $all_rows[]=$rows;
- }
- $this->free();
- return$all_rows;
- }
- ?>
[php]view plaincopy
- //第58题:文件缓存的使用案例
- publicfunction&get($key,$cache_time=‘‘){
- if(empty($cache_time)){
- $cache_time=$this->cache_time;//缓存时间
- }else{
- $cache_time=intval($cache_time);//缓存时间
- }
- $cache_encode_key=$this->get_key($key);//获取一个唯一的长字符串
- //缓存文件的命名和缓存文件的存放位置
- $filename=$this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘.$cache_encode_key.".cache.php";
- if(!is_file($filename)||time()-filemtime($filename)>$cache_time){//如果缓存文件不存在或者缓存文件过期,则返回false
- returnfalse;
- }else{
- returnunserialize(file_get_contents($filename));//如果缓存文件存在且未过期,那么读取缓存文件内容并返回
- }
- }
- publicfunctionset($key,$data){
- $cache_encode_key=$this->get_key($key);
- $cache_dir=$this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘;//缓存文件的缓存目录
- $filename=$cache_dir.$cache_encode_key.".cache.php";//缓存文件名
- if($this->mkpath($cache_dir)){//递归创建缓存目录
- $out=serialize($data);//将要缓存的数据序列化
- file_put_contents($filename,$out);//将序列化的数据写入到缓存文件中
- }
- }
- publicfunctionadd($key,$data){
- $cache_encode_key=$this->get_key($key);
- $cache_dir=$this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘;//缓存目录
- $filename=$cache_dir.$cache_encode_key.".cache.php";//缓存文件名
- if(is_file($filename)){//如果缓存文件存在,则返回false
- returnfalse;
- }else{//如果缓存文件不存在,则生成一个新的缓存文件到缓存目录
- $this->set($key,$data);
- }
- }
- publicfunctiondel($key){
- $cache_encode_key=$this->get_key($key);
- $cache_dir=$this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘;//缓存目录<span></span>
- $filename=$cache_dir.$cache_encode_key.".cache.php";//缓存文件名
- if(is_file($filename)){//如果缓存文件存在则返回false<span></span>
- returnfalse;
- }else{//如果缓存文件不存在,则删除缓存文件
- @unlink($filename);
- }
- }
- publicfunctionget_key($key){
- returnmd5(CACHE_KEY.$key);//md5加密字符串
- }
[php]view plaincopy
- //第59题:redis使用案例
- //推送活动商品上线
- publicfunctionpush_activity_goods_online($data){
- $key=sprintf($this->config->item(‘activity_goods_push_key‘),$data[‘goods_id‘]);
- $this->cache->redis->delete($key);
- $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
- return$this->cache->redis->hmset($key,$data);
- }
- //拉取上线活动商品
- publicfunctionpull_activity_goods_online($key){
- $key=sprintf($this->config->item(‘activity_goods_push_key‘),$key);
- $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
- return$this->cache->redis->hgetall($key);
- }
- //删除上线的活动商品
- publicfunctiondel_activity_goods_online($key){
- $key=sprintf($this->config->item(‘activity_goods_push_key‘),$key);
- $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
- return$this->cache->redis->delete($key);
- }
- //推送活动上线
- publicfunctionpush_activity_online($data){
- $activity_push_key=$this->config->item(‘activity_push_key‘);
- //非通用平台作用哉活动,活动前缀+平台作用域
- if(isset($data[‘plateform‘])&&($data[‘plateform‘])!=‘all‘){
- $activity_push_key=$data[‘plateform‘].‘:‘.$activity_push_key;
- }
- //订单促销
- if($data[‘activity_range‘]==1){
- $key=sprintf($activity_push_key,‘order‘);
- }else{
- $key=sprintf($activity_push_key,$data[‘activity_id‘]);
- }
- $this->cache->redis->delete($key);
- $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
- return$this->cache->redis->hmset($key,$data);
- }
- //拉取上线活动
- publicfunctionpull_activity_online($key){
- if($key==‘order‘){
- $key=sprintf($this->config->item(‘activity_push_key‘),‘order‘);
- }if($key==‘mobile:order‘){
- $key="mobile:".sprintf($this->config->item(‘activity_push_key‘),‘order‘);
- }else{
- $key=sprintf($this->config->item(‘activity_push_key‘),$key);
- }
- $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
- return$this->cache->redis->hgetall($key);
- }
- //删除上线的活动
- publicfunctiondel_activity_online($key,$activity=array()){
- $activity_push_key=$this->config->item(‘activity_push_key‘);
- //非通用平台作用哉活动,活动前缀+平台作用域
- if(isset($activity[‘plateform‘])&&($activity[‘plateform‘])!=‘all‘){
- $activity_push_key=$activity[‘plateform‘].‘:‘.$activity_push_key;
- }
- $key=sprintf($activity_push_key,$key);
- $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
- //echo"redis->delete:".$key."\r\n<br>";
- return$this->cache->redis->delete($key);
- }
- //获取全部活动
- publicfunctionpull_all_activity_online(){
- $aids=array();
- $key="*".sprintf($this->config->item(‘activity_push_key‘),"*");
- $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
- $hashname=$this->cache->redis->keys($key);
- if(is_array($hashname)){
- $prefix_key=str_replace("%s",‘‘,$this->config->item(‘activity_push_key‘));
- foreach($hashnameas$key=>$value){
- /*if($value==($prefix_key."order")){
- continue;
- }*/
- $aid=str_replace($prefix_key,"",$value);
- $aids[]=$aid;
- }
- return$aids;
- }else{
- returnnull;
- }
- }
- //获取所有活动商品
- publicfunctionpull_all_activity_goods_online(){
- $aids=array();
- $key=sprintf($this->config->item(‘activity_goods_push_key‘),"*");
- $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
- $hashname=$this->cache->redis->keys($key);
- if(is_array($hashname)){
- $prefix_key=str_replace("%s",‘‘,$this->config->item(‘activity_goods_push_key‘));
- foreach($hashnameas$key=>$value){
- $aid=str_replace($prefix_key,"",$value);
- $aids[]=$aid;
- }
- return$aids;
- }else{
- returnnull;
- }
- }
- //获取线上活动商品bygoods_id
- publicfunctionget_activity_goods_online($product_id){
- if(empty($product_id)){
- returnfalse;
- }
- $activity_goods=$this->pull_activity_goods_online($product_id);
- if(!empty($activity_goods)){
- return$activity_goods;
- }
- returnfalse;
- }
[php]view plaincopy
- //第60题:php如何读取php.ini配置文件中的配置选项的值
- echoini_get(‘post_max_size‘);//获取上传文件的最大尺寸
- echoget_cfg_var(‘post_max_size‘);//获取上传文件的最大尺寸
[php]view plaincopy
- //第61题:php中如何动态的调用一个函数
- $name_list=‘小强,张三,李四,王五,马六‘;
- $funName=‘explode‘;//函数名当作字符串
- echo"<pre>";print_r($funName(‘,‘,$name_list));
- echo"<pre>";print_r(call_user_func_array($funName,array(‘,‘,$name_list)));
- /**
- *结果:
- *Array(
- *[0]=>小强
- *[1]=>张三
- *[2]=>李四
- *[3]=>王五
- *[4]=>马六
- *&nbs
知识推荐
- Bootstrap-datepicker3官方文档中文翻译---概述(原版翻译 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
- jquery判断checked的三种方法
- jsp 和 servlet
- React可以渲染html标签 或React组件
- websocket实现简单的通信
- JS onload
- HttpPost请求将json作为请求体传入的简单处理方法
- JS----对象的合并与克隆
- vue.js 图书管理系统
- electron-vue:Vue.js 开发 Electron 桌面应用
- web项目的开发
- js黑魔法
- 实用的 CSS 的高级技巧,总有一个你需要的!
- jenkins上展示html报告【转载】
- php mkdir 777失败
- Ajax实现异步上传图片
- node.js ?在函数内获取当前函数
- HTTP协议【详解】