This is a part of my script:
$one = 0;
$two = 0;
$three = 0;
$data = 'a-b-c';
$data = explode("-", $data);
$one = $data[0];
$two = $data[1];
$three = $data[2];
No problems so far but $data sometimes can be
$data = 'a-b-c';
and sometimes
$data = 'a-b';
In case of $data = 'a-b'
; I get Undefined offset: 3 error. Is it some way how to avoid this error?
Wrap the assignment into an if
-block:
if(isset($data[0])) {
$one = $data[0];
}
...
This now checks if this array item isset, if not you just do not assign it and no error will show up.
Assumes that at least one will always exist
$data = 'a-b';
list($one, $two, $three) = explode("-", $data . '-0-0');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With