Page 1 of 1

PHP [] operator not supported for strings

Posted: Sat May 01, 2004 7:52 am
by whyme_t
My friend has a problem with his final year project, and I can't seem to find a fix for him.

The PHP code causing the problem is

Code: Select all

<?php
//********** minicart init **********
if(!isset($minicart))
{
   if(isset($_GET['minicart'])){
      $minicart = array(unserialize(stripslashes($_GET['minicart']))) ;
   }else{
      $minicart = array() ;
   }
}

$sminicart = serialize($minicart) ;

//************************************

//********** value init **********
if(isset($_GET['value'])){
   $value = $_GET['value'] ;
}else{
   $value = 'none' ;
}
//********************************

$minicart[] = $value ;


$minicart = array_values($minicart) ;

include('index.php') ;

?>
the line

Code: Select all

$minicart[] = $value
is causing the trouble, [] operator not supported for strings.

I've tried searching for a solution, but with no joy.

Anybody have any ideas how to fix this?

P.S This code worked perfectly on localhost, but only failed when uploaded to a real web server

Re:PHP [] operator not supported for strings

Posted: Sat May 01, 2004 7:57 am
by Candy
whyme_t wrote: $minicart[] = $value ;


$minicart = array_values($minicart) ;
If this is your final year, I hope I'm not going to work with you anytime soon. You assign a string to an array (or at least, you pretend to), then you convert the array to an array, parsing it as a string.

Try

Code: Select all

$minicart = array_values($value);

Re:PHP [] operator not supported for strings

Posted: Sat May 01, 2004 10:47 am
by whyme_t
Since when did Mega-Tokyians start posting rude messages to people asking for help :-\

I don't pretend to know PHP in any great detail, and like i said, this is not my project.

I am trying to help someone get finished, and I have searched the problem.
[]= could be considered an Array Operator (in the same way that .= is a String Operator).
[]= pushes an element onto the end of an array, similar to array_push:
So shouldn't

Code: Select all

$minicart[] = $value
push $value onto the end of the array?

And From http://www.php.net/array_push

Code: Select all

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
[rant]And for the record, I don't think I would want to work with someone who clearly wasn't a team player, by being unhelpful and rude. Even if this was a small programming mistake, who here claims to never make mistakes when coding? Sometimes it takes someone fresh from the problem to point out a silly mistake...but lets hope they can do it in a non self righteous way, eh![/rant]

Re:PHP [] operator not supported for strings

Posted: Sat May 01, 2004 11:11 am
by Candy
whyme_t wrote: Since when did Mega-Tokyians start posting rude messages to people asking for help :-\

...

[rant]And for the record, I don't think I would want to work with someone who clearly wasn't a team player, by being unhelpful and rude. Even if this was a small programming mistake, who here claims to never make mistakes when coding? Sometimes it takes someone fresh from the problem to point out a silly mistake...but lets hope they can do it in a non self righteous way, eh![/rant]
Sorry bout that, shouldn't post with a headache. I assume from the unhelpful part that it didn't work?

I didn't mean to be rude, but I was wondering how you ended up with code that attempts an implicit conversion, and then does an explicit conversion from one type to another in one variable.
I don't pretend to know PHP in any great detail, and like i said, this is not my project.

I am trying to help someone get finished, and I have searched the problem.
[]= could be considered an Array Operator (in the same way that .= is a String Operator).
[]= pushes an element onto the end of an array, similar to array_push:
So shouldn't

Code: Select all

$minicart[] = $value
push $value onto the end of the array?

And From http://www.php.net/array_push

Code: Select all

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
That would be so, if minicart already was an array. Because the second function afaik creates an array for you, and you don't have to store the variable first, it just seems a lot more logical to use the first variable as direct input.

Re:PHP [] operator not supported for strings

Posted: Sat May 01, 2004 1:54 pm
by BI lazy
Could you pls tell, what you expect this code to do? I simply don't get it - well, it's late and my brain is tired. But maybe we can work it out together?

@Candy: oooch, you have fire in your words, lad! Need to be careful with ya.

Re:PHP [] operator not supported for strings

Posted: Tue Jun 01, 2004 7:04 am
by Hoyo
The problem is with the use of global vars which might explain why it worked on a different server given different configs.

It seems PHP is getting confused between $_GET['minicart'] and $minicart with REGISTER GLOBALS switched on - rename one of these vars and it should work. Same applies to $_GET['value'] and $value.

Hope this helps -