11

I have this:

list($firstname, $lastname) = explode(' ', $queryString);

Sometiems $lastname does not gets defined, and it's there i am getting undefined offset error.

Because it can not find anything to put in $lastname, i guess.

After the explode() i have:

if(!$lastname) { $lastname = $firstname; }

So my question is how can i define it as the $firstname if $lastname is not defined (if you wrote only 'Adam' and not 'Adam Thompson', the lastname should be defined so it is 'Adam Adam')

It does this for me now, but I am receiving the offset error

Karem
  • 17,615
  • 72
  • 178
  • 278

5 Answers5

35
list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, null);

The 2 in explode() ensures, that there are at most 2 values and array_pad() ensures, that there are at least 2 values. If there is no space character , $lastname is null. This you can use to decide what comes next

$lastname = is_null($lastname) ? $firstname : $lastname;

Little update: For this specific case you can use a little trick

list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, $queryString);

This will do all that in one step. It should work, because

  • There is always at least one value (for $firstname)
  • If there is one value, then $queryString == $firstname. Thats now the value that is used to fill the array up to 2 values (which is exactly one, because one value we already have)
  • If there are two values, then the array is not filled with $queryString, because we already have 2 values

At least for readability I would prefer the first more obvious solution.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 1
    +1 Interesting solution. Although I'd argue it's overkill for such a simple thing. – Jason McCreary Jul 13 '11 at 23:25
  • I can't follow... The first solution just adds one single function call. That's a single function call, not "overkill". Additional `array_pad()` is the function, that does (and is designed for) exactly this use case: Ensuring, that there are at least `x` elements. By saying "its overkill" you also say, that `array_pad()` is completely useless. – KingCrunch Jul 13 '11 at 23:32
  • 1
    Slow down there **King**. That's a bit of a stretch - *overkill = `array_pad()` is useless*. I said no such thing. As I pointed out, it's an interesting solution. But, I maintain 3 function calls when 1 would do is *overkill*. Furthermore, I think storing first name, in a variable called `$lastname` is unintuitive and leads to poor code. I'd rather encourage the OP to do it *better*, than make it work. – Jason McCreary Jul 13 '11 at 23:39
  • @JasonMcCreary I have _two_ function calls (remind, that `list()` is a language construct), which besides are both native C functions, and you have one function call, but an additional `if`-construct. If you can reliable _measure_ a _significant_ difference, than I may agree, that it is overkill. – KingCrunch Aug 13 '12 at 17:40
  • Could someone possibly tell me, why I have 2 servers - one with PHP 5.2.6, and the second with 5.3.3, - and I get the error `Undefined offset` only in 5.3.3, while the same code on the same data works smoothly in 5.2.6? – Stan Sep 09 '13 at 19:59
  • @Stan Probably because of `error_reporting`-settings, but to be honest: I am not sure, if this is related to this question/answer :? – KingCrunch Sep 09 '13 at 20:01
4

Try appending a space:

list($firstname, $lastname) = explode(' ', $queryString . ' ' );

shouldn't have to change a thing after that.

brady.vitrano
  • 2,256
  • 2
  • 16
  • 26
1

I just ran into this today. my solution was not the above, (which had no effect) mine was the following:

while (!feof($fh))
{
    $line = fgets($fh);
    print $line;
}

instead of doing:

while ($line = fgets($fh))
{
     print $line;
}
1

You're not getting an Error, but a Notice.

Although this is acceptable since PHP is a dynamic language, you can prevent it with isset():

if(!isset($lastname)) {
  $lastname = $firstname;
}

UPDATE

Per the comments, list() is the culprit for the Notice. In which case, I wouldn't recommend the use of list() when explode() doesn't yield the appropriate number of parameters.

If you must, the answer by brady or undefined offset when using php explode() can work. Although it's pretty ugly in my opinion. I believe your code would be much more intuitive if you just did the following:

$name = explode(' ', $queryString);

if (isset($name[1])) {
  // show lastname
}
else {
  // show firstname
}
Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
0

I'm not clear why this works, but the notice will go away. First, with this code I get the undefined offset notice:

list($month, $day, $year)=explode('-', $dateToChange, 3);

However, with this code, I don't:

list($month, $day, $year, $junk)=explode('-', $dateToChange.'---', 4);

Also note, with '-' or '--' appended to $dateToChange, I will get the offset notice. It takes three dashes for it to go away in my example with four variables. $junk contains the two dashes (one being a separator).

anagama
  • 11