Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with a piece of PHP coding
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JHanson90




PostPosted: Thu Jul 29, 2004 7:25 pm   Post subject: 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:
code:
$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.
Sponsor
Sponsor
Sponsor
sponsor
octopi




PostPosted: Mon Aug 02, 2004 10:31 am   Post subject: (No subject)

Is $id, an array?
Or is it a string.


$id is different then $id[]
JHanson90




PostPosted: Wed Aug 04, 2004 10:19 pm   Post subject: (No subject)

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




PostPosted: Wed Aug 04, 2004 10:30 pm   Post subject: (No subject)

code:
$id[] = "value";


Is crappy PHP shorthand for:

code:
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




PostPosted: Fri Aug 06, 2004 9:13 pm   Post subject: (No subject)

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..... Razz
wtd




PostPosted: Fri Aug 06, 2004 10:06 pm   Post subject: (No subject)

JHanson90 wrote:
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.

JHanson90 wrote:
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..... Razz


That's fine, as long as it makes sense within the context of the rest of the language. This one doesn't. Smile

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




PostPosted: Sat Aug 07, 2004 4:19 pm   Post subject: (No subject)

wtd wrote:
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:
code:
<?php


class PHP_Test {
       
        var $name;
        var $age;
       
        function name ($setname) {
                $this->name = $setname;
                return $this->name;
        }
        function age ($setage) {
                $this->age = $setage;
                return $this->age;
        }
        function end ($newline) {
                echo $newline;
        }
       
}


?>

And here's the relevant part of index.php:
code:
<?php include("class.php"); ?>
<body>
<table width="750" border="0" align="center" cellpadding="5" cellspacing="1">
<caption align="center">Test PHP Coding</caption>
  <tr>
    <td align="center" valign="middle"><?php
        $bob = array(
                'name' => 'Bob'  ,
                'age' => 49
        );
        $fred = array(
                'name' => 'Fred'        ,
                'age' => 43
        );
        $line_br = "<br />\n";
       
        $guy1 = new PHP_Test;
        echo "1. " . $guy1->name($bob['name']) . "'s age: " . $guy1->age($bob['age']) . $guy1->end($line_br);
        $guy2 = new PHP_Test;
        echo "2. " . $guy2->name($fred['name']) . "'s age: " . $guy2->age($fred['age']) . $guy2->end($line_br);
        ?></td>
  </tr>
</table>
</body>

And for some reason it's turning out like this (view source code to see what I mean): www.jhanson90.jktf.com/PHP
The "<br />\n" is appearing in the wrong place, if you haven't already figured out what's wrong with it.
wtd




PostPosted: Sat Aug 07, 2004 5:22 pm   Post subject: (No subject)

code:
<?php

class PHPTest {
   var $name;
   var $age;
   
   function PHPTest($name, $age) {
      $this->name = $name;
      $this->age  = $age;
   }   
}

?>


code:
<?php
   include("class.php");

   $people = array(
      "Bob" => new PHPTest("Bob", 49),
      "Fred" => new PHPTest("Fred", 43)
   );
?>
<body>
<table width="750" border="0" align="center" cellpadding="5" cellspacing="1">
<caption align="center">Test PHP Coding</caption>
  <tr>
    <td align="center" valign="middle">
      <ol>
      <?php foreach ($people as $name => $person) { ?>
         <li><?= $name ?>'s age: <?= $person->age ?></li>
      <?php } ?>
      </ol>
      </td>
  </tr>
</table>
</body>


Remember, he more code you write, the more you can screw up.
Sponsor
Sponsor
Sponsor
sponsor
JHanson90




PostPosted: Sat Aug 07, 2004 6:06 pm   Post subject: (No subject)

wtd wrote:
code:
$people = array(
      "Bob" => new PHPTest("Bob", 49),
      "Fred" => new PHPTest("Fred", 43)
   );


I don't think that you can set the name and age properties through declaring an object. It has to be like:
code:
$new_object_1 = new Object_name;
$new_object_1->name = 'a name';
$new_object_2 = new Object_name;
$new_object_2->name = 'a different name';


Because trying to set the properties directly would uhh, I dunno, just be like saying (though I might not be so right about this part):
code:
class Classname { var $var1 = 'a name'; var $var2 = 'a different name'; }
That would be pointless because the class would have no methods....

Am I right? Or am I just a noob .......... ?
wtd




PostPosted: Sat Aug 07, 2004 6:26 pm   Post subject: (No subject)

You're right. It's bad object-oriented programming.

You are however wrong in that what I wrote is the PHP4 way of doing things, since PHP4 OO sucks particularly badly. In PHP4 everything is public. There's no encapsulation of data.

Since PHP4 doesn't enforce that anyway, there's no point in writing extra code to access the variables.
JHanson90




PostPosted: Sun Aug 08, 2004 7:13 pm   Post subject: (No subject)

wtd wrote:
You're right. It's bad object-oriented programming.

You are however wrong in that what I wrote is the PHP4 way of doing things, since PHP4 OO sucks particularly badly. In PHP4 everything is public. There's no encapsulation of data.

Since PHP4 doesn't enforce that anyway, there's no point in writing extra code to access the variables.


Would you consider PHP5 to be better with it? I'm reading a book on PHP, and it only covers up to PHP4. Since I just got to the objects chapter, I'm debating whether or not I should bother with it since they changed the object model in PHP5....
wtd




PostPosted: Sun Aug 08, 2004 8:20 pm   Post subject: (No subject)

Yes, PHP5 is better (assuming you're a Java fan, since they copied Java pretty extensively), but if you're going to study PHP you'll have to be intimately familiar with both.

Though PHP5 has a number of improvements, it also won't be adapted very quickly. A lot of people, some of whom may decide to give you money to deal with code, are still going to be using PHP4, likely for years to come.
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: