dongqindan4406 2019-02-16 08:20
浏览 25


I have a PHP script that goes like:-

<?php session_start() ?>

   catch(Exception $e){

with $userId=$_GET['userId']; this being my line number 23.

I'm loading the script using http://localhost/checkIdAbout.php?all=ALL.

Now clearly my URL doesn't have the value for $userId, so it'll give an error and that's understandable to me. What I don't understand is that why isn't the try-catch block coming into play? I already stored the value for $_SESSION['userId2']. So shouldn't during execution the code jump from try to catch and give me the required value for $userId?

I get the error: Notice: Undefined index: userId in /var/www/html/checkIdAbout.php on line 23 and I just can't get what's wrong. Please help. Thanks!

  • 写回答

1条回答 默认 最新

  • dronthpi05943 2019-02-16 10:53

    A notice is not an exception.

    You could write a custom handler to convert notices to exceptions:

    set_error_handler(function ($errno , $errstr, $errfile, $errline) {
        if (!(error_reporting() & $errno)) {
            return false;
        throw new ErrorException($errstr, $errno, $errno, $errfile, $errline);
    }, E_NOTICE);
    try {
    } catch (ErrorException $e) {
        echo 'Notice cast to exception: '. $e->getMessage(), PHP_EOL;

    ... but it's normally a bad idea because throwing an exception interrupts the script flow until next catch (or totally aborts the script if none). And, in this case, I don't think it's justified to intentionally cause a notice just to be able to use exception handling, when there're simpler alternatives, e.g.:

    $userId = filter_input(INPUT_GET, 'userId') ?? $_SESSION['userId2'];
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 关于用abap来解决动态规划的问题,但是要求输出索引值,这个是难点
  • ¥15 在ISIS中什么是IP从地址
  • ¥15 压测时,并发量过高时,响应时间出现尖刺
  • ¥15 关于vmprotect3.8.4虚拟文件一项
  • ¥15 在不用IT调试的情况下怎样能连外网
  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法