-1

I have this code:

$arrayOfData = array("Data1", "Data2", "Data3", "Data4", "Data5", "Data6", "Data7", "Data8", "Data9");

var $dataCounter = 0;
var $setCounter = 1;

foreach($arrayOfData as $row => $value){

    $this->$dataCounter++;

    if($this->$dataCounter == 3){
      $this->$setCounter++;
      $this->$dataCounter = 0;
    }


}

When I goto echo the $this->$setCounter all I get is 0. I am using PHP 4, is my syntax correct? My foreach is inside a function inside a class. My variables are inside the class.

infused
  • 24,000
  • 13
  • 68
  • 78
user1269625
  • 3,121
  • 26
  • 79
  • 111
  • 2
    No. You're not in a class, why are you using `$this`? – h2ooooooo Dec 05 '13 at 19:43
  • 1
    also you dont need to declare var in php. and array of data should be array("data1","data2",etc) – Kai Qing Dec 05 '13 at 19:45
  • My foreach is inside a function inside a class. My variables are inside the class. – user1269625 Dec 05 '13 at 19:50
  • 2
    Why on earth are you using PHP 4!?!?!? – John V. Dec 05 '13 at 19:51
  • How about you paste the actual class. It seems you just picked out random lines and are backpeddling to keep up with what is required to actually fix your code. – skrilled Dec 05 '13 at 19:52
  • @John V.: what if you have terribly (but extremely important) old legacy 100M LoC project (of course without unit tests) that works on php4? What would you do to extend it - continue using php4 or try to upgrade to php5? – zerkms Dec 05 '13 at 19:54
  • 1
    @zerkms I'd wish I wasn't in such a position, but point taken. – John V. Dec 05 '13 at 19:56

3 Answers3

5

Since this doesn't appear to be in a class, no, it is not correct. $this-> is unecessary and incorrect as is your usage of var.

var $setCounter = 1;
// ...
$this->$setCounter++;

just needs to be:

$setCounter = 1;
// ...
$setCounter++;

The same applies to $dataCounter

edit

If you are using a class then you

  1. should move var $dataCounter = 0; var $setCounter = 1; outside of your method.

  2. change $this->$setCounter++; to $this->setCounter++;

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 2
    this is the correct solution. another thing to note is that when you reference a classes properies in php you dont use the variable operator to do so, the correct syntax would be $this->setCounter++ not $this->$setCounter if you were inside of a class – Jake Dec 05 '13 at 19:47
  • My foreach is inside a function inside a class. My variables are inside the class. – user1269625 Dec 05 '13 at 19:49
  • var is still bad practice. it's either public, private, protected, etc. – skrilled Dec 05 '13 at 19:50
  • 1
    @skrilled If they are using PHP4 they don't have much of a choice – John Conde Dec 05 '13 at 19:52
  • 1
    Yeah, just noticed that. Who the hell uses php4 nowadays > – skrilled Dec 05 '13 at 19:53
0

$foo->$bar is a variable variable. You're not dealing with a bar member of the foo object, you're dealing with whatever value is stored in a separate $bar variable, which is used to look up a member of `$foo. e.g.

# Set up object
$foo = new stdClass;
$foo->bar = 'bar';
$foo->foo = 'foo';

# Try to write to something in the object    
$x = 'bar';
$foo->$x = 'hello';  // note the $ on x
$foo->x = 'there';  // note the lack of $

var_dump($foo);

object(stdClass)#1 (3) {
  ["bar"]=>
  string(5) "hello"   // because of $foo->$x
  ["foo"]=>
  string(3) "foo"
  ["x"]=>
  string(5) "there"   // because of $foo->x
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

No need to use the keyword 'var' in PHP like you are.

I believe you need to look back at the basics. Check out the following link for more details: http://www.php.net/manual/en/language.variables.basics.php

Also, you do not need the ->. You only use this if the left part is an object instance. You would use it to access instance members or static members (however, I'd suggest :: for static members). Check out the following link for more details: Reference - What does this symbol mean in PHP?

As far as your code goes, remove the 'arrow's and the 'var's. You are echoing 0 because you initialized 'var $dataCounter' as 0.

    $arrayOfData = array("Data1", "Data2", "Data3", "Data4", "Data5", "Data6", "Data7", "Data8", "Data9");

    $dataCounter = 0;
    $setCounter = 1;

    foreach($arrayOfData as $row => $value){

           $dataCounter++;

           if($dataCounter == 3) {
                $setCounter++;
                $dataCounter = 0;
           }
   }

Start from there and then continue. Also, for what you are doing a foreach is unnecessary. Instead:

    $arrayOfData = array("Data1", "Data2", "Data3", "Data4", "Data5", "Data6", "Data7", "Data8", "Data9");

    $setCounter = (sizeOf($arrayOfData))%3; //counts sets of 3
Community
  • 1
  • 1
womplefrog
  • 769
  • 1
  • 6
  • 18