4

I'm getting this error in my OpenCart log. Says the error is on line 1 which is:

<?php if(isset($social_discount['name']) && $social_discount['name']!="") { ?>

Would highly appreciate any help to fix this.

5
  • and this error is ? Mar 12, 2013 at 17:26
  • @hexblot PHP Notice: Uninitialized string offset: 0 :p
    – Tchoupi
    Mar 12, 2013 at 17:26
  • code added, forgot to put in the pre tags
    – Chris Judd
    Mar 12, 2013 at 17:27
  • 4
    are you sure $social_discount is an array? Mar 12, 2013 at 17:27
  • im still quite new to php so im not sure, it should be
    – Chris Judd
    Mar 12, 2013 at 17:28

2 Answers 2

6

To prevent this error, you should change your code like the following:

<?php if(is_array($social_discount) && isset($social_discount['name']) && $social_discount['name']!="") { ?>
3
  • leave in the isset() too, just in case of undefined index 'name'
    – Waygood
    Mar 12, 2013 at 17:31
  • You're right, edited.
    – ole
    Mar 12, 2013 at 17:33
  • that seemed to work! thank you!
    – Chris Judd
    Mar 12, 2013 at 17:38
2

$socialdiscount is a string, not an array, so $social_discount['name'] is treated as $social_discount[0] => first character of the string. 0 is not set means $socialdiscount is an empty string.

2
  • so how should that line of code actually be?
    – Chris Judd
    Mar 12, 2013 at 17:31
  • There is nothing wrong with that line of code. However, I suspect there is something seriously wrong with the lines before that line, which make your $socialdiscount variable an empty string "" rather then the expected array.
    – Wrikken
    Mar 12, 2013 at 18:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.