jaunty_mellifluous Posted September 12, 2008 Share Posted September 12, 2008 Can you tell me why this code doesn't work? Quote <?php $myarray = array('php', 'is', 'cool'); for($i = 0; $i < count($myarray); $i++) { echo "The value of index $i is: {$myarray[$i]} <br />\n"; ?> Link to comment https://forums.phpfreaks.com/topic/123986-solved-simple-array-traversal/ Share on other sites More sharing options...
akitchin Posted September 12, 2008 Share Posted September 12, 2008 try adding a closing brace to your for() statement. it's a syntax error. also, for the record, you would have an easier time using foreach(). if you need to use for() for whatever reason, you should put the count() statement out of the loop (and use a variable instead) since otherwise it has to run that function on every iteration. Link to comment https://forums.phpfreaks.com/topic/123986-solved-simple-array-traversal/#findComment-640054 Share on other sites More sharing options...
jaunty_mellifluous Posted September 12, 2008 Author Share Posted September 12, 2008 thanks buddy .. is count() function only used to count all the elements in an array? Or are there any other uses for it? Link to comment https://forums.phpfreaks.com/topic/123986-solved-simple-array-traversal/#findComment-640057 Share on other sites More sharing options...
DarkWater Posted September 12, 2008 Share Posted September 12, 2008 That's all it's used for. Link to comment https://forums.phpfreaks.com/topic/123986-solved-simple-array-traversal/#findComment-640060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.