duanchendu69495 2016-12-04 20:50
浏览 64
已采纳

PHP error_log仍然可以在switch中使用break

I thought i had a bug in my code but by isolating the problem i found out some 'weird' behaviour of error_log() inside a switch case.

I added some error_log for debugging, but then wehenever i was running throught the switch, the error_log would always be triggered even if the call was made outside of the valid case.

Here the exact part of my code that made me find out this wierdness:

switch($LLTP->request()){

  case 'json':
    /* some code commented out for debug here */
    error_log('json');
    echo 'json';
    break;

  default:
    /* some code commented out for debug here */
    error_log('html');
    echo 'html';
    break;
}

the request method is plain and simple and return a string like 'json','html','ajax' etc..

 public function request(){
    return $this->requests;
  }

so if the switch is html, the default kick in and i get the expepted results:

echoing 'html' (expected)

errorlog 'html' (expected)

BUT when i run it with the 'json' switch, i get the expected 'json' on screen and inside the error_log, but i also get the 'html' error entry (without the echo).

echoing 'json' (expected)

errorlog 'json' (expected)

errorlog 'html, referer: https://www.example.com/home.json' (unexpected)

Is this normal behavior? Fisrt time i notice it and if i try to reproduce the 'weirdness' with another sample code:

$vv='json';
switch($vv){
  case 'json':
    echo 'json';
    error_log('json');
    break;
  default:
    echo 'html';
    error_log('html');
    break;
}

i get, (in the errorlog)

[error] json (expected)

[error] json, referer: http://www.example.com/home.json (unexpeted as it's the second entry in the log)

i'm lost, there something i don't understand or expected results is not what I expected.

PS: i don'T output the errors on the page, i only log errors in the logs. if this had anything to do with it o.O

展开全部

  • 写回答

1条回答 默认 最新

  • dst67283 2016-12-11 00:59
    关注

    Today i found out the reason behind this.

    When i was testing i was spoofing a json request with my browser. But everytime i would hit refresh, my browser would also send a request for the favicon.ico file. Since i didn't add the mod_rewrite rules for images it was handled as an regular html request by the code, therefore calling the html part of the switch.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥80 有偿!!请懂注册表的翻译解析一下一小段注册表的调整内容
  • ¥15 基于52单片机 交通灯系统
  • ¥15 根据访客ip显示城市名称
  • ¥20 对文档进行操作,有偿 有意向的可以加我v
  • ¥15 brainstorm进行致痫指数分析
  • ¥30 beeline连接hive集群会卡住
  • ¥15 julia语言画表面图
  • ¥15 前端css轮播图效果优化
  • ¥15 如何在已有的土地利用类型图中加入新的地类呢
  • ¥20 TCIA数据库下载报错,请问如何解决
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部