Jump to content

First string in array doesn't show?


3raser

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/232927-first-string-in-array-doesnt-show/
Share on other sites

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/>";
	}
}

?>

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. :/

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.