0

When I try to add a first item it gives "Notice: Undefined offset: 0" as an error because of the fact that the array is empty, how can I let the title be "Test" the first time I add an item? The array also needs to be emptied sometimes so it should still work after that

$result = $query->getResult();

    if(($result[0]->getId())>0){
        $title = "Test" . ($result[0]->getId() + 1);
    }

    else{
        $title = "Test";
    }
T.Goose
  • 1
  • 1
  • 3
    Use `count($result) > 0` to test if the array has values. You might also want an `isset` and / or `is_array`, but it really depends. – Jonnix Feb 19 '18 at 12:10
  • Like @JonStirling said, user `count` or `ìsset` or `!empty()` to check if the array contains data before trying to access the first element of the array (0) – teeyo Feb 19 '18 at 12:15
  • do like this:- `$result = $query->getResult(); if(count($result)>0){ if($result[0]->getId() > 0){ $title = "Test" . ($result[0]->getId() + 1); }else{ $title = "Test"; } }` – Alive to die - Anant Feb 19 '18 at 12:15

0 Answers0