samedi 8 mars 2014

Undefined offset: 1 when checking if isset in function


Vote count:

0





$s = "text";
$exploded_s = explode('/', $s);
//$s2 = isset_or($exploded_s[1], "empty"); //This shows notice Undefined offset: 1
$s2 = isset($exploded_s[1]) ? $exploded_s[1] : "empty"; //This is ok
echo $s2;

function isset_or($var, $val){
return isset($var) ? $var : $val;
}


Both return "empty" but the first way also shows the notice. Why?



asked 2 mins ago

Loclip

1,196

1 Answer



Vote count:

0




You get an Undefined Offset here



$s2 = isset_or($exploded_s[1], "empty");


That is because the $s had no / inside of it , so the array $exploded_s probably wont have anything inside it.



answered 10 secs ago





Aucun commentaire:

Enregistrer un commentaire