
-----------------------------------
JHanson90
Thu Jul 29, 2004 7:25 pm

Help with a piece of PHP coding
-----------------------------------
I'm not sure if this is enough info, if not I'll put in some more source.  But here's line 79 of cv.php:
$id[] = 'news-area';

And here's the error I get when I view the page that includes cv.php:
Fatal error: [] operator not supported for strings in /data/hosted/jhanson90/projects/empire/cv.php on line 79

Wtf?  Of course it's not an operator... I don't know what it means or what I'm supposed to do about it.

-----------------------------------
octopi
Mon Aug 02, 2004 10:31 am


-----------------------------------
Is $id, an array?
Or is it a string.


$id is different then $id[]

-----------------------------------
JHanson90
Wed Aug 04, 2004 10:19 pm


-----------------------------------
The problem was sort of fixed, but I don't know why.  $id is an array, and doing $id[] = 'value'; I'm adding another key and value to the array.  It still makes no sense why it gave me that error.  It was fixed by putting $id = array(); before it.

-----------------------------------
wtd
Wed Aug 04, 2004 10:30 pm


-----------------------------------
$id[] = "value";

Is crappy PHP shorthand for:

array_push($id, "value");

Basically you're adding "value" onto the end of that array.  However, in order to do this, an array named $id has to already exist.  If it doesn't... KA-BLOOEY!

-----------------------------------
JHanson90
Fri Aug 06, 2004 9:13 pm


-----------------------------------
Yeah I know.  But it's always worked for me before, without having to set the array.  PHP randomly decided to give me that error on that day.

I know that the proper way to write it is array_push();, but who wants to continually write that over and over again?  I wouldn't call it crappy shorthand, because it does the exact same thing as far as I know.  The entire reason computers were made were to speed things up and make stuff more efficient.....  :P

-----------------------------------
wtd
Fri Aug 06, 2004 10:06 pm


-----------------------------------
Yeah I know.  But it's always worked for me before, without having to set the array.  PHP randomly decided to give me that error on that day.

Be careful about taking advantages of bugs.  Eventually someone will fix them and your code will break horribly.

I know that the proper way to write it is array_push();, but who wants to continually write that over and over again?  I wouldn't call it crappy shorthand, because it does the exact same thing as far as I know.  The entire reason computers were made were to speed things up and make stuff more efficient.....  :P

That's fine, as long as it makes sense within the context of the rest of the language.  This one doesn't.  :-)

If only PHP made it possible to overload operators, had an object-oriented base library, or used multimethod dispatch, this wouldn't be an issue.  In terms of polymorphism, though, PHP's on par with C.

-----------------------------------
JHanson90
Sat Aug 07, 2004 4:19 pm


-----------------------------------
had an object-oriented base library

Yeah speaking of "object-oriented," I'm not.  I've just started learning about PHP's OOP structure, and I'm having some problems with my test scripts.
Here's class.php:
And here's the relevant part of index.php:


Test PHP Coding
  
    