Warning: Illegal offset type

Question :

Warning: Illegal offset type 
- This warning message can appear when you use a Function, an Array, an Object, or other incorrect value as the index /key of an Array element.

$id = function(){ return 8; }; 

$aray[$id] = 'https://coursesweb.net/'; 

// $id contains a function // OR: $id = array(); 

$aray[$id] = 'https://marplo.net/'; 

// $id is an Array


- Check the code at the line indicated in the error message, and use a variable that contains a string, or numeric value for the index /key of the array element.
--You get this error when use a  not  valid array key (I assume it's an object of some sorts). Array keys can only be strings or numbers.

function fName(){ return 8; } 
// $id contains the result returned by the function 

$id = fName(); 

$aray[$id] = 'https://coursesweb.net/'; 

// OR: $id = 'id'; $aray[$id] = 'https://marplo.net/';

if you want to improve the solution
comment to help the community
No comments
Post a Comment

Post a Comment