-1

I have generated this attached file uploaded here: https://files.fm/u/uqm4marg This file was generated using PHP. \t was used to insert a tab space. It has worked find through out the entire file but for the tab space between INR0564 D and INR0564 C. This tab space only has one white space while other tab spaces have 4 like between 667722553331 and INR0564.

So the question is while \t was used to give a tab space between characters why is there different spacing at different places.

The below image is taken on sublime text which shows the different tab spaces mentioned above.

Community
  • 1
  • 1
Nihilarian
  • 1,190
  • 1
  • 10
  • 17

3 Answers3

3

The tab character does not insert spaces. It is a single character that is interpreted by the editor.

The purpose of the tab character is to help writing data in tabular format, i.e. in table format. Since the rows are already provided by the lines of text, the tab character helps creating columns.

The editors interpret the tab character as moving the cursor to the right not one position (as it happens with the space character) but until it reaches the next tabulator stop.

In computer software, the tabulator stops are usually set at each 8 or 4 columns. This means either columns 1, 9, 17 a.s.o. or 1, 5, 9, 13 etc. In your picture (and from the description in your question) it seems the "size" of your tab you are using is 4 columns (this is the mostly used value nowadays).

Because the tab character sends the cursor to a fixed position, its perceived "size" is not fixed, it depends of the cursor column before the tab character.

In your image, the first column of data has 12 characters (it is displayed on columns 1-12 in the editor). Then, the tab character sends the cursor to the next tabulator that is on column 17. The next value (INR0564, 7 characters) is displayed in columns 17-23. The next tab character is "shorter", it covers only 1 column; it sends the cursor to column 25 where is displayed D. The next one makes the cursor jump to column 29 (3 columns) where nothing is displayed. Then there is another tab that spans 4 characters until the next tabulator (in column 33) where the 2.0SEp Salary value starts.

The columns, the tabulators anb and your data are depicted below:

1   5   9   13  17  21  21  25  29  33 ...
v   v   v   v   v   v   v   v   v   v  ...
056665554531    INR0564 D       2.0Sep Salary
            .   |      .|.  |   |
            +-->|      >|+->+-->|

In the above drawing, a dot (.) marks the position of the cursor after each column of data and a pipe (|) marks its position after the tab character was displayed. The "arrows" on the last line depict how the cursor jumped over multiple columns because of the tab characters, to reach the next tabulator.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • When I open this in sublime text or textmate or in notepad it shows me the same format, so I think it has nothing to do with the editors. Can we not tell PHP to make a consistent tabular space indentation of 4 spaces ? – Nihilarian Sep 01 '17 at 12:41
  • Have you read my answer? Read it again. Then take a closer look to the image you posted. There are only 3 empty columns after `D` (for the first tab then another 4 columns for the second tab. And PHP is not involved in the way the editors display the tab character. If you want to always have 4 spaces between the columns then use 4 spaces and not tabs. – axiac Sep 01 '17 at 12:53
1

Your editor is configured to advance the cursor to the next tab stop. With a tab width configured to 4 characters, tab stops are at line offsets 0, 4, 8, a.s.o. It should be understandable, that a horizontal tab on offset 11 will advance to offset 12, and not to 15 (11 + 4). It's just not a fixed width, but must be seen in relation to fixed stops.

marabu
  • 1,166
  • 7
  • 9
1

Tabs Spaces has nothing to do with if you have given correct \t in code, It has generated correctly. The behaviour tab is differs with type of editor.

For Instance , If you use below code for generation

 $myfile = fopen(getcwd()."/newfile.txt", "w") or die("Unable to open 
                  file!");
            $txt = "BATBALL88026613\n
                    667722553331\tINR0564D\tSeptember\t2.00Salary\n
                    051122334432\tINR0564C\tSeptember\t1.00Salary\n
                    056401112233\tINR0564C\tSeptember\t1.00Salary";
            fwrite($myfile, $txt);
            fclose($myfile);

And you try to open in NOTEPAD and NOTEPAD++ it will be different. That inconsistency will not be there in NOTEPAD++.

praveen
  • 46
  • 5
  • There is no inconsistency. Notepad puts tabulator marks at every `8` columns (the "size" of a `tab` character is `8` in Notepad), Notepad++ uses `4`, like most of the other editors nowadays. – axiac Sep 01 '17 at 10:54