分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

PHP常见面试题汇总(二)

发布时间:2023-09-06 01:27责任编辑:赖小花关键词:PHP面试题

PHP常见面试题汇总(二)

  1. //第51题:统计一维数组中所有值出现的次数?返回一个数组,其元素的键名是原数组的值;键值是该值在原数组中出现的次数
  2. $array=array(4,5,1,2,3,1,2,"a","a");
  3. $ac=array_count_values($array);
  4. /**
  5. *输出结果:
  6. *Array(
  7. *[4]=>1
  8. *[5]=>1
  9. *[1]=>2
  10. *[2]=>2
  11. *[3]=>1
  12. *[a]=>2
  13. *)
  14. */
  15. echo"<pre>";print_r($ac);
[php]view plaincopy
  1. //第52题:如何统计字符串中每种字符的出现次数并排序?
  2. functioncountStr($str){
  3. $str_array=str_split($str);//str_split()函数能将字符串中每个字符都转化为数组的元素
  4. $str_array=array_count_values($str_array);
  5. arsort($str_array);//arsort()函数对数组进行逆向排序并保持索引关系
  6. return$str_array;
  7. }
  8. $str="asdfgfdas323344##$\$fdsdfg*$**$*$**$$443563536254fas";
  9. /**
  10. *输出结果:
  11. *Array(
  12. [$]=>7
  13. [3]=>6
  14. [*]=>6
  15. [4]=>5
  16. [f]=>5
  17. [s]=>4
  18. [d]=>4
  19. [5]=>3
  20. [a]=>3
  21. [6]=>2
  22. [2]=>2
  23. [g]=>2
  24. [#]=>2
  25. )
  26. */
  27. echo"<pre>";print_r(countStr($str));
[php]view plaincopy
  1. //第53题:str_word_count()函数计算字符串中的单词数?
  2. /**
  3. *输出结果:2
  4. */
  5. echostr_word_count("Helloworld!");//参数二:默认0,返回单词的数目
  6. /**
  7. *输出结果:
  8. Array(
  9. [0]=>Hello
  10. [1]=>world
  11. )
  12. */
  13. echo"<pre>";print_r(str_word_count("Helloworld!",1));//参数二:1-返回包含字符串中的单词的数组
  14. /**
  15. *输出结果:
  16. Array(
  17. [0]=>Hello
  18. [6]=>world
  19. )
  20. */
  21. echo"<pre>";print_r(str_word_count("Helloworld!",2));//参数二:2-返回一个数组,其中的键是单词在字符串中的位置,值是实际的单词.
[php]view plaincopy
  1. <?php
  2. /**
  3. *第56题:redis与memcached区别?
  4. *
  5. *不同点:
  6. *
  7. *(1)redis中并不是所有数据在有效期内只能常驻内存的(如果需要,可定期同步持久化到磁盘),这是和memcached相比一个最大的区别(memcached中的数据在有效期内是以键值对的形式常驻内存的)
  8. *(2)redis不仅仅支持简单的键值对类型的数据,同时还提供list,set,hash等数据结构的存储;memcached仅支持简单的键值对类型的数据,但是memcached却可以缓存图片、视频等等数据
  9. *(3)redis支持数据的备份,即master-slave模式的数据备份
  10. *(4)redis支持数据的持久化和数据同步,可以将内存中的数据保存在磁盘中,重启系统后可以再次加载进行使用,缓存数据不会因此而丢失.memached缓存数据是常驻内存的,重启系统后数据就没了
  11. *(5)redis可以通过expire设定有效期,memcached在set数据的时候可以指定要缓存的数据永不过期
  12. *(6)redis可以做一主多从;memcached也可以做一主多从
  13. *(7)redis当物理内存用完时,可以将一些很久没用到的value交换到磁盘;memcached当物理内存用完后就会自动清理一些早期的数据
  14. *
  15. *相同点:
  16. *
  17. *(1)redis和memcached都是将数据存放在内存中,都是内存数据库
  18. *(2)redis和memcached都可以做一主多从
  19. *
  20. *性能:
  21. *
  22. *(1)redis根据其官方的测试结果:在50个并发的情况下请求10w次,写的速度是110000次/s,读的速度是81000次/s
  23. *(2)redis将键名与值的最大上限各自设定为512MB;memcached则<span></span>将键名限制在250字节,值也被限制在不超过1MB,且只适用于普通字符串.
  24. *
  25. *何时使用memcached:
  26. *
  27. *(1)小型静态数据:当我们需要缓存小型静态数据的时候可以考虑memcached,最具代表性的例子就是HTML代码片段;因为memcached的内部内存管理机制虽然不像redis的那样复杂,但却更具实际效率,这是因为memcached在处理元数据时所消耗的内存资源相对更少.作为memcached所支持的惟一一种数据类型,字符串非常适合用于保存那些只需要进行读取操作的数据,因为字符串本身无需进行进一步处理<span></span>.
  28. *
  29. */
  30. ?>

[php]view plaincopy
  1. <?php
  2. //第57题:memcached的使用案例
  3. //第一步:获取数据
  4. functionget_data(){
  5. $sql="SELECT*fromorder_master";//一般来说都是超级复杂的sql语句
  6. return$this->db->cache_all($sql);//获取缓存数据(如果缓存数据过期,则重新查询数据库并将查询到的数据放到memcache中并返回)
  7. }
  8. //第二步:获取memcache中缓存数据
  9. publicfunctioncache_all($sql,$reload=false,$limit=null){//参数1-sql语句、参数2-是否从数据库中重新获取数据并写入memcache、参数3-每次获取记录数量
  10. $this->reload=$reload;
  11. $sql=$this->get_query_sql($sql,$limit);//拼接sql语句,如:‘select*fromorder_masterlimit0,10‘
  12. return$this->get_cache($sql,‘get_all‘);//参数1-拼接的sql、参数2-要执行的方法名;将sql语句作为参数调用get_all()方法
  13. }
  14. //第三步:从memcache中获取缓存数据提示:1、如果缓存数据过期,则重新查询数据库并将查询结果放到memcache缓存中,返回数据.2、如果缓存数据未过期,则直接获取缓存数据
  15. protectedfunctionget_cache($sql,$method){
  16. $cache_file=md5($sql.$method);//memcache中缓存数据的键名,格式:键名=md5(sql语句+方法名)提示:一定要保证键名唯一
  17. $res=$this->cache->get($cache_file);//根据缓存数据的键名获取键值
  18. if(!$res){//memcache中缓存数据过期的情况//如果memcache中缓存数据过期,则重新从数据库中获取数据,并将结果以键值对形式存储到memcache中
  19. $res=$this->$method($sql);//调用$this->get_all($sql);//直接从数据库中获取数据并返回
  20. if($res&&$this->cache_mark&&!$this->reload){
  21. $this->cache->set($cache_file,$res);//将数据库中查询到的数据以键值对形式set到memcache缓存中
  22. }
  23. }
  24. return$res;//返回数据
  25. }
  26. //第四步:直接从数据库中获取数据并返回
  27. publicfunctionget_all($sql,$limit=null,$fetch_mode=MYSQLI_ASSOC){
  28. $this->query($sql,$limit);
  29. $all_rows=array();
  30. $this->fetch_mode=$fetch_mode;
  31. while($rows=$this->fetch()){
  32. $all_rows[]=$rows;
  33. }
  34. $this->free();
  35. return$all_rows;
  36. }
  37. ?>
[php]view plaincopy
  1. //第58题:文件缓存的使用案例
  2. publicfunction&get($key,$cache_time=‘‘){
  3. if(empty($cache_time)){
  4. $cache_time=$this->cache_time;//缓存时间
  5. }else{
  6. $cache_time=intval($cache_time);//缓存时间
  7. }
  8. $cache_encode_key=$this->get_key($key);//获取一个唯一的长字符串
  9. //缓存文件的命名和缓存文件的存放位置
  10. $filename=$this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘.$cache_encode_key.".cache.php";
  11. if(!is_file($filename)||time()-filemtime($filename)>$cache_time){//如果缓存文件不存在或者缓存文件过期,则返回false
  12. returnfalse;
  13. }else{
  14. returnunserialize(file_get_contents($filename));//如果缓存文件存在且未过期,那么读取缓存文件内容并返回
  15. }
  16. }
  17. publicfunctionset($key,$data){
  18. $cache_encode_key=$this->get_key($key);
  19. $cache_dir=$this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘;//缓存文件的缓存目录
  20. $filename=$cache_dir.$cache_encode_key.".cache.php";//缓存文件名
  21. if($this->mkpath($cache_dir)){//递归创建缓存目录
  22. $out=serialize($data);//将要缓存的数据序列化
  23. file_put_contents($filename,$out);//将序列化的数据写入到缓存文件中
  24. }
  25. }
  26. publicfunctionadd($key,$data){
  27. $cache_encode_key=$this->get_key($key);
  28. $cache_dir=$this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘;//缓存目录
  29. $filename=$cache_dir.$cache_encode_key.".cache.php";//缓存文件名
  30. if(is_file($filename)){//如果缓存文件存在,则返回false
  31. returnfalse;
  32. }else{//如果缓存文件不存在,则生成一个新的缓存文件到缓存目录
  33. $this->set($key,$data);
  34. }
  35. }
  36. publicfunctiondel($key){
  37. $cache_encode_key=$this->get_key($key);
  38. $cache_dir=$this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘;//缓存目录<span></span>
  39. $filename=$cache_dir.$cache_encode_key.".cache.php";//缓存文件名
  40. if(is_file($filename)){//如果缓存文件存在则返回false<span></span>
  41. returnfalse;
  42. }else{//如果缓存文件不存在,则删除缓存文件
  43. @unlink($filename);
  44. }
  45. }
  46. publicfunctionget_key($key){
  47. returnmd5(CACHE_KEY.$key);//md5加密字符串
  48. }
[php]view plaincopy
  1. //第59题:redis使用案例
  2. //推送活动商品上线
  3. publicfunctionpush_activity_goods_online($data){
  4. $key=sprintf($this->config->item(‘activity_goods_push_key‘),$data[‘goods_id‘]);
  5. $this->cache->redis->delete($key);
  6. $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
  7. return$this->cache->redis->hmset($key,$data);
  8. }
  9. //拉取上线活动商品
  10. publicfunctionpull_activity_goods_online($key){
  11. $key=sprintf($this->config->item(‘activity_goods_push_key‘),$key);
  12. $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
  13. return$this->cache->redis->hgetall($key);
  14. }
  15. //删除上线的活动商品
  16. publicfunctiondel_activity_goods_online($key){
  17. $key=sprintf($this->config->item(‘activity_goods_push_key‘),$key);
  18. $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
  19. return$this->cache->redis->delete($key);
  20. }
  21. //推送活动上线
  22. publicfunctionpush_activity_online($data){
  23. $activity_push_key=$this->config->item(‘activity_push_key‘);
  24. //非通用平台作用哉活动,活动前缀+平台作用域
  25. if(isset($data[‘plateform‘])&&($data[‘plateform‘])!=‘all‘){
  26. $activity_push_key=$data[‘plateform‘].‘:‘.$activity_push_key;
  27. }
  28. //订单促销
  29. if($data[‘activity_range‘]==1){
  30. $key=sprintf($activity_push_key,‘order‘);
  31. }else{
  32. $key=sprintf($activity_push_key,$data[‘activity_id‘]);
  33. }
  34. $this->cache->redis->delete($key);
  35. $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
  36. return$this->cache->redis->hmset($key,$data);
  37. }
  38. //拉取上线活动
  39. publicfunctionpull_activity_online($key){
  40. if($key==‘order‘){
  41. $key=sprintf($this->config->item(‘activity_push_key‘),‘order‘);
  42. }if($key==‘mobile:order‘){
  43. $key="mobile:".sprintf($this->config->item(‘activity_push_key‘),‘order‘);
  44. }else{
  45. $key=sprintf($this->config->item(‘activity_push_key‘),$key);
  46. }
  47. $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
  48. return$this->cache->redis->hgetall($key);
  49. }
  50. //删除上线的活动
  51. publicfunctiondel_activity_online($key,$activity=array()){
  52. $activity_push_key=$this->config->item(‘activity_push_key‘);
  53. //非通用平台作用哉活动,活动前缀+平台作用域
  54. if(isset($activity[‘plateform‘])&&($activity[‘plateform‘])!=‘all‘){
  55. $activity_push_key=$activity[‘plateform‘].‘:‘.$activity_push_key;
  56. }
  57. $key=sprintf($activity_push_key,$key);
  58. $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
  59. //echo"redis->delete:".$key."\r\n<br>";
  60. return$this->cache->redis->delete($key);
  61. }
  62. //获取全部活动
  63. publicfunctionpull_all_activity_online(){
  64. $aids=array();
  65. $key="*".sprintf($this->config->item(‘activity_push_key‘),"*");
  66. $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
  67. $hashname=$this->cache->redis->keys($key);
  68. if(is_array($hashname)){
  69. $prefix_key=str_replace("%s",‘‘,$this->config->item(‘activity_push_key‘));
  70. foreach($hashnameas$key=>$value){
  71. /*if($value==($prefix_key."order")){
  72. continue;
  73. }*/
  74. $aid=str_replace($prefix_key,"",$value);
  75. $aids[]=$aid;
  76. }
  77. return$aids;
  78. }else{
  79. returnnull;
  80. }
  81. }
  82. //获取所有活动商品
  83. publicfunctionpull_all_activity_goods_online(){
  84. $aids=array();
  85. $key=sprintf($this->config->item(‘activity_goods_push_key‘),"*");
  86. $this->load->driver(‘cache‘,array(‘adapter‘=>‘redis‘));
  87. $hashname=$this->cache->redis->keys($key);
  88. if(is_array($hashname)){
  89. $prefix_key=str_replace("%s",‘‘,$this->config->item(‘activity_goods_push_key‘));
  90. foreach($hashnameas$key=>$value){
  91. $aid=str_replace($prefix_key,"",$value);
  92. $aids[]=$aid;
  93. }
  94. return$aids;
  95. }else{
  96. returnnull;
  97. }
  98. }
  99. //获取线上活动商品bygoods_id
  100. publicfunctionget_activity_goods_online($product_id){
  101. if(empty($product_id)){
  102. returnfalse;
  103. }
  104. $activity_goods=$this->pull_activity_goods_online($product_id);
  105. if(!empty($activity_goods)){
  106. return$activity_goods;
  107. }
  108. returnfalse;
  109. }
[php]view plaincopy
  1. //第60题:php如何读取php.ini配置文件中的配置选项的值
  2. echoini_get(‘post_max_size‘);//获取上传文件的最大尺寸
  3. echoget_cfg_var(‘post_max_size‘);//获取上传文件的最大尺寸
[php]view plaincopy
  1. //第61题:php中如何动态的调用一个函数
  2. $name_list=‘小强,张三,李四,王五,马六‘;
  3. $funName=‘explode‘;//函数名当作字符串
  4. echo"<pre>";print_r($funName(‘,‘,$name_list));
  5. echo"<pre>";print_r(call_user_func_array($funName,array(‘,‘,$name_list)));
  6. /**
  7. *结果:
  8. *Array(
  9. *[0]=>小强
  10. *[1]=>张三
  11. *[2]=>李四
  12. *[3]=>王五
  13. *[4]=>马六
  14. *&nbs
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved