undefined offset when using php explode()

this is because your fullname doesn't contain a space. You can use a simple trick to make sure the space is always where

 $split = explode(' ', "$fullname ");

(note the space inside the quotes)

BTW, you can use list() function to simplify your code

  list($first, $last) = explode(' ', "$fullname ");

Use array_pad

e.q.: $split = array_pad(explode(' ', $fullname), 2, null);

  • explode will split your string into an array without any limits.
  • array_pad will fill the exploded array with null values if it has less than 2 entries.

See array_pad

Tags:

Php

Explode