Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.