#### Archived

This topic is now archived and is closed to further replies.

# Undefined offset: 2 (Problem With Function)

## Recommended Posts

So I have another problem.
What I'm pretty much trying to do is to make a program that displays text. So I've made a function to do this, because it extracts the text from the file "songs.inc". The songs are formatted in the file in this way: "Song Title#", (using the # to split the songs up). Here's the function that's meant to display the songs:
[code]
function display_songs($file_path) {$all_songs = fopen($file_path, "r");$songs = explode("#", $all_songs);$songs = str_replace("#", "<br />", $songs);$song_count = count($songs); for ($i = 0; $i <=$song_count; $i += 1) { echo "$songs[$i] In My Pants"; } } [/code] When I run the code though, all it displays is [quote]Resource id 4 Notice: Undefined offset: 2 in c:\program files\easyphp1-8\www\in my pants\includes\functions.inc.php on line 41[/quote] Any Ideas? #### Share this post ##### Link to post ##### Share on other sites [quote]$song_count = count($songs); for ($i = 0; $i <=$song_count; $i += 1) {[/quote] if you 20 songs,$i needs to go from 0 to 19. When you reach 20 in your code you get the undefined offset.

Try
[hr]
[code=php:0]
function display_songs($file_path) {$all_songs = file_get_contents($file_path);$songs = explode("#", $all_songs); foreach ($songs as $song) { echo "$song In My Pants<br>";
}
}
[/code][hr]