2

I have one .txt file and one .php file

.txt:

15118553;1315239266;0;1;EURUSD;1.4111;0;0;1315239282;1.4109;0;0;-27;0;

.php:

<?php
$text = file_get_contents('test.txt', true);
$sir = explode(";", $text);
$d=count($sir);

for($i=0; $i<=$d ; $i++) 
    echo "string = ". $i ."-------". $sir[$i]. "<br>" ;
?>

in localhost :

string = 0-------15118553
string = 1-------1315239266
string = 2-------0
string = 3-------1
string = 4-------EURUSD
string = 5-------1.4111
string = 6-------0
string = 7-------0
string = 8-------1315239282
string = 9-------1.4109
string = 10-------0
string = 11-------0
string = 12--------27
string = 13-------0
string = 14-------

Notice: Undefined offset: 15 in C:\xampp\htdocs\test\index.php on line 7
string = 15-------

Why do they display 15 strings? In the txt file there are only 13 strings. How do I fix this error?

1
  • Tip: do not use for to iterate arrays. Use foreach. Commented Aug 8, 2014 at 9:11

4 Answers 4

2

Why "Undefined offset":

Change it to:

for($i=0; $i < $d ; $i++) Because you are starting to count elements from zero, not from one.

Why 14:

Because your input string ends with the delimiter, so after exploding you get jne extra string.

You can either cycle to $d - 1, or use an if condition to check if this string is an empty string.

Sign up to request clarification or add additional context in comments.

1 Comment

Trimming the value of $text is needed as well to avoid printing empty string @pos 14.
1

That error means that you do not have offset '15' in $sir array.

You should iterate until $count - 1.

But as you are a beginner for skipping this types of issues for array always use foreach.

Try like this:

 foreach($str as $strValue) {
    echo "string = ". $strValue. "<br>" ;
}

Comments

0
for($i=0; $i<$d ; $i++) 

loop will run from 0 to n-1

Comments

0

Because array start at the index 0.

So, when you count lines by yourself, you start : "1, 2, 3, 4,..." when an array starts "0, 1, 2, 3 ...". So you have a 15th line that appears, but crashes because $d[15] = null;

Moreover, you wrote "$i<=$d". And $d[14] = "", because it is between ;and the end of the line. You can echo "Count : ".$d, and see Count : 14.

To understand better :

Line[x] | Array[x] | count($sir) | Array <= $d // $d = count($sir);

   1          0          14          TRUE
   2          1          14          TRUE
   3          2          14          TRUE
   4          3          14          TRUE
   5          4          14          TRUE
   6          5          14          TRUE
   7          6          14          TRUE
   8          7          14          TRUE
   9          8          14          TRUE
   10         9          14          TRUE
   11         10         14          TRUE
   12         11         14          TRUE
   13         12         14          TRUE
   14         13         14          TRUE
   15         14         14          TRUE  // --> Array[14] == "". PHP prints empty string, but doesn't crash.
   16         15         14          FALSE // --> Array[15] == null. PHP crashes.

That's why $i (which equals Array[x] here) stacks until $i = 15 before crashing.

You might want to write :

for($i = 0;$i < $d-1;i++)
{
// do something;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.