4

After upgrading from PHP5 to PHP7 I get an error page saying:

0 - [] operator not supported for strings

Error message I've read that now PHP7 is smarter with exception. But the message I get is totally useless. I don't know which file and line to look for the problem. PHP error log is also empty.

How can I get some useful error message? I'd be happy to get something like old nice fatal error saying where to fix it.

P.S. I didn't solve it from the point of view of PHP, but enabling Joomla debug in Global configuration seems to add the lost PHP error message to the output. At least this helped at another web-site in a similar situation.

7
  • Do you have error reporting set to "development"?
    – Lodder
    Mar 21, 2017 at 9:46
  • Yes, as well as PHP settings to output full errors. Mar 21, 2017 at 17:37
  • 1
    The error message you get is not useless @Gruz; [] operator not supported for strings in other words means the [] operator, which happens to be the shorthand or short array syntax for array() as of PHP versions 5.4, cannot be used on strings. Mar 22, 2017 at 8:45
  • @Gruz: I have followup questions based on your comment on my answer to your question: How can I get some useful error message?. What version of Joomla are you using for your project? Any theme and or extensions in use? For error printed like the old nice fatal error: where do you have your website hosted? In any case, should your concern not be answered, please consider pointing out clearly what they are for more contributions. Mar 22, 2017 at 9:17
  • I managed to find the place manually, but it doesn't matter. My question is how to find the problematic file and page if it's hidden because of the exception handler. Not "what this error means in PHP and how to fix it", but how to find the place. Mar 23, 2017 at 9:39

3 Answers 3

4

For Joomla on php7+ non-fatal errors no longer contain the file and line in the error. The trick is to handle the output of these errors in your template's error.php file. Add the file if it doesn't exist.

With a number of templates (including beez) they will show you the stacktrace (and thus also the location of the call the threw the error) if you have debugging on, but you can customise when and how it throws the messages.

For example you could make an error.php file like:

if ($_SERVER['REMOTE_ADDR'] == $your_ip){
  echo "Error Code: " . $this->error->getCode();
  echo "<br>";
  echo htmlspecialchars($this->error->getMessage(), ENT_QUOTES, 'UTF-8');
  echo "<br><br>";
  echo $this->renderBacktrace();
}
3
  • thanks for the explanation! I was able to edit error.php and make it show the file and line (through renderBacktrace()). But is there a way to log non-fatal errors to php error log?
    – ihtus
    Mar 28, 2018 at 14:01
  • for reference here is my similar question stackoverflow.com/questions/49520257/…
    – ihtus
    Mar 28, 2018 at 14:02
  • Thanks for the explanation! I've just met such a problem in backend at isis template. I've examined the error.php code and found out that Joomla debug (not only php debug) should be enabled to show useful information. Mar 28, 2018 at 21:31
1

The error you got after your PHP version upgrade says it loud:

the [] operator not supported for strings.

You are probably trying to push an array into a string or something of that sort.

You would have gotten an Array to string conversion error should the string in question be empty (depending on your new PHP version and as well as defined error reporting level).

The [] operator not supported for strings error occurs when you are re-declaring a string as an array or attempting to use a string as if it were an array.

Take a look at the example below (resulting in the error you have):

$me   = 'foo'; // $me is a variable holding a string value ('foo')
$me[] = 'bar'; // here, $me is re-declared as an array; an attempt to update $me with $me[]

How can you resolve this?

You simply have to re-declare your string variable just as such (still as a string variable; not as an array) for use should need be.

Take a close look at this:

$me = 'foo'; // with 'foo' as initial value when first declared
$me = 'bar'; // expected result on print_r($me) (successful, with no error): bar

Above, $me was re-assigned a new string ('bar') as value successfully and without any error.

Also, do not attempt something funny like trying to still pushing in (into your string variable, whether re-declared or not) an array or else you will get the same error.

Instead, you may convert your string into array should you want to manipulate your values as such for the purpose you seek to achieve.

2
  • It seems you have not read my question. Mar 22, 2017 at 8:38
  • I did @Gruz and it reads How can I get some useful error message?. My answer above highlight the fact that the error message you got, ([] operator not supported for strings) is a very useful. You expressed after your question wanting to see something like old nice fatal error saying where to fix it. I did not see that as a question but now that it is clear it is of concern to you, more information is needed on that. Where do you host your website? What version of Joomla are you using on your project? Mar 22, 2017 at 8:55
-1

If you can access the back end, change the Error Reporting in Global Configuration - Server to 'Maximum'. That setting will display a meaningful PHP error message which would give more information what is going wrong.

If you do not see an error-message please check if the value error_reporting is set to E_ALL in the php.ini: error_reporting=E_ALL.

1
  • I use full errors output and it works in other cases at this web-site. But in this cases the error is catched and just seems don't pass to the regular PHP output. Mar 21, 2017 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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