We help IT Professionals succeed at work.

fundamental PHP foreach question!

Colin Brazier
on
Weird!  Hopefully this is an easy 1000 points for someone.

This is taken straight from the manual, and I get an error - anyone see it?  I'm using CodeIgniter if that's relevant at all.

syntax error, unexpected '$v' (T_VARIABLE) in C:\xampp\htdocs1\fobg\application\controllers\week_fixtures.php on line 31

public function date_chosen()
      {            
            $a = array(1, 2, 3, 17);

            foreach ($a as $v) {
                echo "Current value of \$a: $v.\n";
            }
      }

also flagged up in Dreamweaver:
DW screenshot
Comment
Watch Question

Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
Works for me

Try changing $v to something else
Try putting $v.\n in string in { }
echo "Current value of \$a: {$v}.\n";

Try deleting and retyping line 31.

Author

Commented:
Hi Julian, this is what I get with
public function date_chosen()
	{		
		$a = array(1, 2, 3, 17);		
	}

Open in new window

Call to undefined function  array() in C:\xampp\htdocs1\fobg\application\controllers\week_fixtures.php on line 29

Author

Commented:
I restarted Apache and DW no longer flags an error, but despite delete cache and hard reload the error persists.

In Chrome and Edge.

I need to go out for a couple of hours. Will reboot when I get back.

Cheers

Author

Commented:
Absolutely bizarre.
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
There's fundamentally nothing wrong with the code you've posted in isolation. I've just pasted it straight into a CodeIgniter controller and it worked as it should.

Sometimes errors point to line numbers that just don't make sense, so I would take a look at the controller code as a whole. Remove parts if necessary to double-check that the error isn't actually being caused by code further up the file ... it happens sometimes !!

Author

Commented:
Thanks Chris, I'm new to CI so that must be the underlying reason.  I'll go back and revisit it.

Cheers

Author

Commented:
Well I stripped it and it seems OK now.  What a waste of a morning.

Thanks both.