Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php 5.4 String offset cast occurred

Tags:

php

on the following lines I get the error 'String offset cast occured'

$code[$value['dictionaryAlias']] = $value['dictionaryText'][$codeLang];

$code[$value['dictionaryAlias']] = $value['dictionaryText'][$langDefault];

Actually, the code access a table. It works in previous version but not in 5.4

I am a newbie and I am in charge in converting the code. What changes should I make to make it work. I read that it is probably because the variable is a string instead of an array. What can I do then?

like image 718
klark Avatar asked May 22 '13 17:05

klark


1 Answers

This means that $value['dictionaryText'] is a string, and either $codeLang or $langDefault is not an integer. The indexing operator works on strings, but only accepts integer indices: when it receives something else, it tries to convert it to an integer, most often resulting in the value 0, and returns the character at that index.

This is a new warning that they introduced in 5.4 because it was a frequent mistake and a frequent cause of headaches.

like image 193
zneak Avatar answered Nov 14 '22 06:11

zneak