php check undefined offset

Solutions on MaxInterview for php check undefined offset by the best coders in the world

showing results for - "php check undefined offset"
Magdalena
24 Jul 2016
1// $key = 10, if $array does not have $array[10] set, it will return false
2// If $array[10] has any value, it will return true
3if(array_key_exists($key, $array)){}
Elias
27 Jul 2017
1If preg_match did not find a match, $matches is an empty array. So you should check if preg_match found an match before accessing $matches[0], for example:
2
3function get_match($regex,$content)
4{
5    if (preg_match($regex,$content,$matches)) {
6        return $matches[0];
7    } else {
8        return null;
9    }
10}