-2

I am not talking about this:

How to avoid undefined index

i am talking about a crash, fatal error. Not about a "notice".

EDIT even if this is caused by a notice, the result is a crash. My question is: how to configure laravel, so it does not crash on notices?

I run laravel 5.3 and php 7.

My error:

ErrorException in UploadController.php line 149:
Undefined index: AS

my code:

 if (!PlumConstants::$plum_us_states[$state]) {
        $state = '';
      }

Referencing my constants class:

    class PlumConstants {
    public static $plum_us_states = array(
        'AL' => 'Alabama',
        'AK' => 'Alaska',
        'AZ' => 'Arizona',
        'AR' => 'Arkansas',
        'CA' => 'California',
//        ...
      );

    }

well, that's a bit funny no? because what I am doing is checking if the entry exists with

 if (!PlumConstants::$plum_us_states[$pstate]) {

but somehow php seems to panic and crash because there was not a defined index... when that is exactly what I am doing and avoiding in my code. Is there a way to switch this odd behavior off? i can probably use isset() but i think it's ugly. Because isset does not mean it is not null or empty. What is the nice way to do this?

Community
  • 1
  • 1
Toskan
  • 13,911
  • 14
  • 95
  • 185
  • I know its ugly, but there's a function that check if key is existing. if (array_key_exists( $pstate , PlumConstants::$plum_us_states )) { ... } – Fappie. Sep 12 '16 at 23:05
  • 2
    You're misunderstanding -an *ErrorException* is raised by Laravel because PHP emitted the notice. This is just the [error handler promoting it to an exception](http://php.net/manual/en/class.errorexception.php#errorexception.examples) (it *will* throw the notice as an ErrorException) which you then don't catch. If you address it like it was a notice, the exception won't be thrown. – HPierce Sep 12 '16 at 23:32
  • "I am not talking about this" As @HPierce correctly notes, yes, you are. – ceejayoz Sep 12 '16 at 23:37
  • @ceejayoz haha :D very funny :) oh you marked it as duplicate? you were not even joking... This is clearly a different question, this question here is related to laravel, php 7 and their way of handling notices. Can you point out where that is handled in the other question? – Toskan Sep 14 '16 at 01:08
  • @Toskan You have an undefined index. The solution is the same. Notice vs. error is irrelevant - Laravel in PHP5 chooses to surface notices as errors. It's up to your error handler. – ceejayoz Sep 14 '16 at 01:09
  • @ceejayoz you seem to misunderstand the word 'the same'. In this case though, we get a completely different output, and the application crashes. Does that happen with the undefined index notice in a normal application as well? Correct me if I am wrong. Furthermore, can you point me where HPierce's answer is in the other thread? the thing about laravel raising ErrorExcpetion by laravel. I would be very thankful. – Toskan Sep 14 '16 at 01:17
  • @ceejayoz so how can i disable the crashing on notices? that is in laravel 5.3. Is that written in the other question as well? sometimes i am overlooking quite a bit – Toskan Sep 14 '16 at 01:20
  • @Toskan Don't disable notices. Fix the issue. The linked question includes `array_key_exists` and `isset` approaches to doing so that fix your issue. – ceejayoz Sep 14 '16 at 01:56
  • @ceejayoz that doesn't solve the problem. `if(isset(PlumConstants::$plum_us_states[$pstate]) && PlumConstants::$plum_us_states[$pstate])` would work. Is really ugly though – Toskan Sep 14 '16 at 08:07
  • @Toskan What's wrong with just plain `if(isset(PlumConstants::$plum_us_states[$pstate])`? You're not going to have `'NY' => false` are you? – ceejayoz Sep 14 '16 at 13:37
  • @ceejayoz well replace `$plum_us_states` with `$user_input` – Toskan Sep 16 '16 at 21:59
  • @ceejayoz I accepted ban17 answer as I use it now almost everywhere in my code. Maybe you should reevaluate the way you put peoples question on hold and argue your point for the sake of being scrum master. Just maybe... – Toskan Sep 23 '16 at 00:03

2 Answers2

1

If you are using php7 you can do that using

PlumConstants::$plum_us_states[$pstate] ?? false

instead

!PlumConstants::$plum_us_states[$pstate] 
ban17
  • 634
  • 8
  • 12
0
  1. All right, you doesn't have 'AS' key in your example.
  2. In the first code sample you specified $state variable, in the second - $pstate
  3. Try check like if (!array_key_exists($pstate, PlumConstants::$plum_us_states)) {