3raser Posted April 7, 2011 Share Posted April 7, 2011 I'm trying to expand my knowledge on some more str type functions, and I've always wanted to learn implode and explode, but I was just too lazy. Anyways, for some reason, if I type in: salad,pizza,apples - the first value in the array, salad, doesn't show up. o.O <?php $favorite_food = $_POST['favorite_food']; if(!$favorite_food) { ?> <form action="words.php" method="POST"> <input type="text" name="favorite_food"> <input type="submit"> </form> <?php } else { $ex = explode(",", $favorite_food); $amount = count($ex) + 1; while($i < $amount) { echo $ex[$i]."<br/>"; ++$i; } } ?> I know arrays start at 0, so thats why I added 1 to the count. Quote Link to comment https://forums.phpfreaks.com/topic/232927-first-string-in-array-doesnt-show/ Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 $i is undefined so first time around its not 0 but just nothing, soon as you add 1 it becomes 1, so you dont see the array[0] if that makes sense Quote Link to comment https://forums.phpfreaks.com/topic/232927-first-string-in-array-doesnt-show/#findComment-1198001 Share on other sites More sharing options...
dcro2 Posted April 7, 2011 Share Posted April 7, 2011 There's two problems here. First, $i doesn't have a value before it's used, so it's automatically false when you first use it. Since there's no $ex[false], nothing is outputted. That's why it's preferrable to use a for() loop for these kind of things. Setting error_reporting to E_ALL also helps to catch these kinds of things. Then there's the problem that by adding 1 to the count you're making it echo one more value than actually exists. It makes sense if you really think about it. If you have an array with 3 members, count() will return 3, but your array will have values at [0]-[2]. If you check the source of the page in your browser, you'll see that there's an extra "<br />" at the end. So here's my suggestion: <?php $favorite_food = $_POST['favorite_food']; if(!$favorite_food) { ?> <form action="words.php" method="POST"> <input type="text" name="favorite_food"> <input type="submit"> </form> <?php } else { $ex = explode(",", $favorite_food); $amount = count($ex); for($i = 0; $i < $amount; $i++) { echo $ex[$i]."<br/>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232927-first-string-in-array-doesnt-show/#findComment-1198004 Share on other sites More sharing options...
3raser Posted April 7, 2011 Author Share Posted April 7, 2011 There's two problems here. First, $i doesn't have a value before it's used, so it's automatically false when you first use it. Since there's no $ex[false], nothing is outputted. That's why it's preferrable to use a for() loop for these kind of things. Setting error_reporting to E_ALL also helps to catch these kinds of things. Then there's the problem that by adding 1 to the count you're making it echo one more value than actually exists. It makes sense if you really think about it. If you have an array with 3 members, count() will return 3, but your array will have values at [0]-[2]. If you check the source of the page in your browser, you'll see that there's an extra "<br />" at the end. So here's my suggestion: <?php $favorite_food = $_POST['favorite_food']; if(!$favorite_food) { ?> <form action="words.php" method="POST"> <input type="text" name="favorite_food"> <input type="submit"> </form> <?php } else { $ex = explode(",", $favorite_food); $amount = count($ex); for($i = 0; $i < $amount; $i++) { echo $ex[$i]."<br/>"; } } ?> Thanks, this is much appreciated. I thought by adding one, that would fix the problem with $i. I forgot about for loops for some reason. :/ Quote Link to comment https://forums.phpfreaks.com/topic/232927-first-string-in-array-doesnt-show/#findComment-1198010 Share on other sites More sharing options...
kenrbnsn Posted April 7, 2011 Share Posted April 7, 2011 You can also do this with a foreach loop: <?php $ex = explode(",", $favorite_food); foreach ($ex as $food) { echo "$food<br/>"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/232927-first-string-in-array-doesnt-show/#findComment-1198049 Share on other sites More sharing options...
dcro2 Posted April 7, 2011 Share Posted April 7, 2011 You can also do this with a foreach loop: <?php $ex = explode(",", $favorite_food); foreach ($ex as $food) { echo "$food<br/>"; } ?> Ken Haha, so true. Doh! Quote Link to comment https://forums.phpfreaks.com/topic/232927-first-string-in-array-doesnt-show/#findComment-1198051 Share on other sites More sharing options...
QuickOldCar Posted April 7, 2011 Share Posted April 7, 2011 You can also do this with a foreach loop: <?php $ex = explode(",", $favorite_food); foreach ($ex as $food) { echo "$food<br/>"; } ?> Ken That's the way I do it and prefer. Quote Link to comment https://forums.phpfreaks.com/topic/232927-first-string-in-array-doesnt-show/#findComment-1198074 Share on other sites More sharing options...
3raser Posted April 7, 2011 Author Share Posted April 7, 2011 Lol thanks guys, I'll keep that as a note. Quote Link to comment https://forums.phpfreaks.com/topic/232927-first-string-in-array-doesnt-show/#findComment-1198078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.