0

I'm trying to run a foreach loop in php to go through each line of a csv file, I actually got the setup for this loop from another post on stackoverflow (but it's also described the same on php.net).

My code is:

$csvFile = file('MajorCourses.csv')

foreach($csvFile as &$line)
{
    $data[] = str_getcsv($line)
} 

And the php file is in the same directory as my php file, but I get this error:

"Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in C:\wamp2\www\advisingApp.php on line 23"

I'm not sure why it's "unexpected", I'm new to php. But the loop is in php tags.

user3505195
  • 33
  • 1
  • 1
  • 8

3 Answers3

2

You're missing a semi colon at the end of your code.

use this instead.

$csvFile = file('MajorCourses.csv');

foreach($csvFile as &$line)
{
    $data[] = str_getcsv($line);
} 
1

The loop is "unexpected" because you're missing semicolons at the ends of your statements.

This line:

$csvFile = file('MajorCourses.csv')

should be:

$csvFile = file('MajorCourses.csv');

And:

$data[] = str_getcsv($line)

Should be:

$data[] = str_getcsv($line);
Dave Ross
  • 3,313
  • 1
  • 24
  • 21
1

And typo here:

$csvFile = file('MajorCourses.csv')

should be:

$csvFile = file('MajorCourses.csv');

semicolon missing.

This was indicated by the "unexpected 'foreach' " in the error message. If you get an unexpected error,you should check the code in front of the unexpected.

BitAccesser
  • 719
  • 4
  • 14