duanchendu69495 2016-12-05 04: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 08: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.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分