Jump to content

count function


meltingpoint

Recommended Posts

the file (db_file.txt)contains four things:

monkey

apple

orange

pear

 

when I use    $XX = file("db_file.txt", "r");

and then print_r($XX);

it returns- Array([0]=>monkey[1]=>apple[2]=>orange[3]=>pear[4]=> )

using $size = count($XX);

it gives - 5

 

So why is it giving a 5th element to the file?

 

Link to comment
Share on other sites

Because arrays start at indexing at 0, not 1.

 

Say you have:

 

$array = Array('number1', 'number2', 'number3');

 

$array[0] = number1

$array[1] = number2

$array[2] = number3

 

There's 3 elements, but the highest index is 2.

Link to comment
Share on other sites

Because there is an erroneous new line character after the last line... so it reads the blank line as well... that is why the 4th index is blank.

 

Put the cursor on the line below the last item and backspace it up so the cursor is behind the last piece of data and it should correct it.

Link to comment
Share on other sites

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.