PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 4, 2022

[FIXED] How to set to throw an exception for access array offset on value of type null in php

 August 04, 2022     exception, laravel, php     No comments   

Issue

I found a different behaviour when I run the following code in Laravel and in a simple php script.

try { 
    $a=null; $a[3]; 
    var_dump('ok'); 
} catch (\Exception $e) { 
    var_dump('error'); 
}

In Laravel it returns error but in the simple php script it returns ok.

I was wonder how can I set to return error in php script also.


Solution

This happens because PHP classifies "accessing an array offset on type null" as a warning and not as an exception. Since you only try to catch exceptions and are given warnings, it will never enter the catch block. Since under the hood Laravel translates warnings into exceptions, the catch block will be entered in Laravel and therefore output error.

This answer will explain how to catch warnings.

Pretty sure you can also try your hand at editing the register() method in Handler.php to create a custom error handler, but there are probably plenty of guides online.



Answered By - geertjanknapen
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing