-3

I'm working on some a project where there was never a warning/notice checked. I ran into this problem:

This code gives a notice:

Notice: Undefined index: $key on line 123

$someArray[$key] .= $someString;

I've changed it to this:

if(array_key_exists($key, $someArray)){
  $someArray[$key] .= $someString;
}
else{
  $someArray[$key] = $someString;
}

And now it works, but I don't understand why. Can someone explain this behaviour?

If I'm not clear enough please tell me (it's my first question here)

EDIT: Thanks for your answers! concatenation needs a key to be there, but adding doesn't need a key. PHP interputs a concatenation in the right way, so in my loop it's only triggerd the first time (for the same keys). I understand a bit more, thanks!

EDIT 2: My full php file is 1000 lines of code. But in short it's something like:

$someArray = array();
$key = 'foo';
$someString = 'bar';

if(array_key_exists($key, $someArray)){
  $someArray[$key] .= $someString;
}
else{
  $someArray[$key] = $someString;
}

echo print_r($someArray);
Robbert
  • 1
  • 2
  • 1
    `.=` is concatenation. If the key doesn't exist, then what are you concatenating *to*? That's where the warning is triggered. – deceze Jul 02 '21 at 13:59
  • `$key` does not exists in `$someArray` is that simple. Since you were trying to append to that specific key you need to have that key already, otherwise you need to add it (like you're doing in your else statement). – nitrin0 Jul 02 '21 at 13:59
  • Please provide more information. Your error means that ```$key``` is not an index in ```$someArray```. What's the value of ```$someArray``` and ```$key```? – Stefano Jul 02 '21 at 14:00
  • Please share more details, like a full code example that triggers the problem. Also, the given code would not yield the error `Undefined index: $key` unless you would use `$someArray['$key']` (mind the added quotes) – Nico Haase Jul 02 '21 at 14:08
  • @Nico That *is* the full example… – deceze Jul 02 '21 at 14:09
  • My full code is 1000 lines... I will try a edit, with a bit more detail. – Robbert Jul 02 '21 at 14:09
  • @deceze if that was the full example, it would also yield a notice about the undefined variables `$someArray`and `$someString` – Nico Haase Jul 02 '21 at 14:09
  • @Nico Fine, but assuming `$someArray` does exist, the problem is self explanatory. We don't need to see a sample of an empty `$someArray` to deduce the problem here. As has already happened sufficiently. – deceze Jul 02 '21 at 14:11
  • I added some more details. This was probably something really obvious for long time programmers, but for me it was a learning point... concatination wants something that exists, thats why i needed to check for it. – Robbert Jul 02 '21 at 14:17

2 Answers2

1
$someArray[$key] .= $someString;

Means "take the value of $someArray[$key] and concat $someString to it." So $someArray[$key] has to have a value to start with.

$someArray[$key] = $someString;

Means "Set the $someArray[$key] to equal the value of $someString" So it doesnt care if $someArray[$key] already had a value or not.

DevWithZachary
  • 3,545
  • 11
  • 49
  • 101
0

Let me explain. If the $someArray has got $key as key, then $someArray[$key] .= $someString; this works fine. But if $someArray doesn't have got $key as a key, then $someArray[$key] = $someString; this code is correct.

In a word, you can't use concatenation to undefined value of array. Good luck!

pullidea-dev
  • 1,768
  • 1
  • 7
  • 23