By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

Problem with updating a array member variable in function.

P: 4
Hi

I am new to php.

I have a query. I have a class which has a array member variable list1 and list2. Am trying to update it in a function and not able to.

Here is the class. Please help me.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class simple {
  3.    var $list1;
  4.    var $list2;
  5.  
  6.         function updateValue() {
  7.                 $fd = fopen("/mapping", "r");
  8.                 $i=0;
  9.  
  10.                 while(!feof($fd)) {
  11.                         $info = fscanf($fd, "%s %s\n");
  12.                         if ($info) {
  13.                                 list($name, $version) =$info;
  14.                                 $this->list1[$i] = $name;
  15.                                 $this->list2[$i] = $version;
  16.                                 $info = null;
  17.                                 $i++;
  18.                         }
  19.                 }
  20.                 fclose($fd);
  21.                 echo "$this->list1[0]";
  22.                 echo "$this->list1[1]";
  23.                 echo "$this->list1[2]";
  24.         }
  25.   }
  26. ?>
Thanks & Regards
VP
Aug 19 '08 #1
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
Hi.

What is it exactly that this code is doing?
How is that different from what you expect it to do?

Are you getting any errors?
If not, see this topic.
Aug 19 '08 #2

P: 4
Hi

I am getting the output as Array(0), Array(1) and Array(2). After executing the method.

I have enabled the errors and it does not show anything. Is it the right way of assigning array values.

Please help me.

Regards,
VP
Aug 19 '08 #3

P: 4
Hi

I am getting the output as Array(0), Array(1) and Array(2). After executing the method.

I have enabled the errors and it does not show anything. Is it the right way of assigning array values.

Please help me.


Regards,
VP
Hi

To be clear more with problem. I have a file with following values:

dev0 1.2.3
dev1 2.4.5
dev2 2.6.7

I want to read the dev0, dev1 and dev2 to get stored in list1 array and1.2.3, 2.4.5 and 2.6.7 in to list2 array of the class object.

If list1 and list2 were local variables, I was able to get the desired output.
When i use the class variables . I get the output as Array(0), Array(1) and Array(2) instead of dev0, dev1, dev2.

Thanks
VP
Aug 19 '08 #4

Atli
Expert 5K+
P: 5,058
Ahh ok.

The quotes are causing the error.

When you are echoing a variable, there is no need to surround it by quotes. It can, as in this case, even cause problems.

Try doing:
Expand|Select|Wrap|Line Numbers
  1. echo $this->list[0];
  2.  
The reason why you got the output you did is because the parser doesn't accept the [0] behind the $this->list as a part of the variable.

So, it prints $this->list, and because that is an array, it prints Array, followed by the [0].

You can add array elements inside double-quotes tho, you just need to encapsulate them in curly-braces. Like:
Expand|Select|Wrap|Line Numbers
  1. echo "Value: {$this->list[0]}";
  2. echo "Value: {$this->list['something']";
  3.  
Also, when you are adding elements to an array the way you do, there is no need to specify the index of the element.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. while(!feof($fh)) {
  2.   $this->list[] = fgets($fh);
  3. }
  4.  
This would create a zero-indexed array containing an element for each line in the file.
Aug 19 '08 #5

P: 4
Ahh ok.

The quotes are causing the error.

When you are echoing a variable, there is no need to surround it by quotes. It can, as in this case, even cause problems.

Try doing:
Expand|Select|Wrap|Line Numbers
  1. echo $this->list[0];
  2.  
The reason why you got the output you did is because the parser doesn't accept the [0] behind the $this->list as a part of the variable.

So, it prints $this->list, and because that is an array, it prints Array, followed by the [0].

You can add array elements inside double-quotes tho, you just need to encapsulate them in curly-braces. Like:
Expand|Select|Wrap|Line Numbers
  1. echo "Value: {$this->list[0]}";
  2. echo "Value: {$this->list['something']";
  3.  
Also, when you are adding elements to an array the way you do, there is no need to specify the index of the element.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. while(!feof($fh)) {
  2.   $this->list[] = fgets($fh);
  3. }
  4.  
This would create a zero-indexed array containing an element for each line in the file.
Hi Atli,

It worked ... Thanks for the reply.

Regards,
VP
Aug 19 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.