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);