Undefined variable is certainly defined

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mads-hk
Forum Newbie
Posts: 1
Joined: Fri Apr 06, 2012 5:17 am

Undefined variable is certainly defined

Post by mads-hk »

Greetings!

I have narrowed my problem down to this bit:

Code: Select all

<?php
$productgroupfile = file_get_contents("./product-groups.txt");
$productgroups = explode("\n",$productgroupfile);
foreach ($productgroups as $productgroup) {
	echo "<a href=\"link.html\">".$productgroup."</a> <br />";
}
?>
And it throws the following errors(line 13, is the line with the foreach loop):
Notice: Undefined variable: productgroups in /Applications/MAMP/htdocs/bau/product-groups.php on line 13

Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/bau/product-groups.php on line 13
The txt file contains:
product1
product2
product3
I have been staring at those five lines for at few hours, and i seriously can't figure out the problem... Maybe i have just missed a semi-colon or something, maybe im just blind!

Thanks in advance,

Mads
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined variable is certainly defined

Post by Celauran »

Have you done a var_dump of $productgroups? Doesn't Mac use a different line end? \r or something?
Post Reply