nulltest.txtの内容
あ\n \n \t\n \n ""\n 123\n abc\n "00"\n \n
geditで最終行に改行を入れる(上記の例ではカーソルが"00"の次の行にいくようにする)と改行だけの行ができる。Windowsのエディタでは多くの場合そうならない。
eof()を使って読み込むとさらにもう一回読んでしまう。
| line⇨⇦ | bin2hex | strlen | type | empty | is_null | trimed_line | strlen | type | trimed == null | empty | is_null |
| ⇨あ ⇦ | e381820a | 4 | string | FALSE | FALSE | ⇨あ⇦ | 3 | string | FALSE | FALSE | FALSE |
| ⇨ ⇦ | 0a | 1 | string | FALSE | FALSE | ⇨⇦ | 0 | string | TRUE | TRUE | FALSE |
| ⇨ ⇦ | 090a | 2 | string | FALSE | FALSE | ⇨⇦ | 0 | string | TRUE | TRUE | FALSE |
| ⇨ ⇦ | 200a | 2 | string | FALSE | FALSE | ⇨⇦ | 0 | string | TRUE | TRUE | FALSE |
| ⇨"" ⇦ | 22220a | 3 | string | FALSE | FALSE | ⇨""⇦ | 2 | string | FALSE | FALSE | FALSE |
| ⇨123 ⇦ | 3132330a | 4 | string | FALSE | FALSE | ⇨123⇦ | 3 | string | FALSE | FALSE | FALSE |
| ⇨abc ⇦ | 6162630a | 4 | string | FALSE | FALSE | ⇨abc⇦ | 3 | string | FALSE | FALSE | FALSE |
| ⇨"00" ⇦ | 223030220a | 5 | string | FALSE | FALSE | ⇨"00"⇦ | 4 | string | FALSE | FALSE | FALSE |
| ⇨ ⇦ | 0a | 1 | string | FALSE | FALSE | ⇨⇦ | 0 | string | TRUE | TRUE | FALSE |
| ⇨⇦ | 0 | boolean | TRUE | FALSE | ⇨⇦ | 0 | string | TRUE | TRUE | FALSE |
trim()は\n,\t,スペースなどを削除するが、結果長さ0の文字列になったかは==で検査できる。これはempty()と等価のようだ。===だとすべてFALSEになる。is_null()に相当する。eof()で読んでしまう最後の行は不思議な値だが、trim()すると長さ0の文字列になる。
PHPでは長さ0の文字列とnullは異なる概念らしい。詳細はwebのマニュアル型の比較表で確認できる。
変数は、以下の場合に NULL とみなされます。 ・定数 NULL が代入されている場合。 ・まだ値が何も代入されていない場合。 ・unset() されている場合。
そのプログラムがこれ。<table></table>はそのまま書き、その間にPHPプログラムを書きます。
<table>
<?php
$fp = fopen("nulltest.txt","r");
$arr_ct = 0;
while(!feof($fp)){
$line = fgets($fp);
$tpline = gettype($line);
$lnline = strlen($line);;
$rime = trim($line);
$tprime = gettype($rime);
$lnrime = strlen($rime);;
print "<tr>\n";
print "<td><b>⇨</b>$line<b>⇦</b></td>";
print "<td>" . bin2hex($line) . "</td>";
print "<td>$lnline</td><td>$tpline</td>";
//print "<td>" . empty($line) . "</td>";
echo empty($line) ? '<td>TRUE</td>': '<td>FALSE</td>';
//print "<td>" . is_null($line) . "</td>";
(is_null($line)) ? print '<td>TRUE</td>': print '<td>FALSE</td>';
print "<td><b>⇨</b>$rime<b>⇦</b></td>";
print "<td>$lnrime</td><td>$tprime</td>";
($rime == null) ? print "<td>TRUE</td>": print "<td>FALSE</td>";
//print "<td>" . empty($rime) . "</td>";
echo empty($rime) ? '<td>TRUE</td>': '<td>FALSE</td>';
//print "<td>" . is_null($rime) . "</td>";
(is_null($rime)) ? print '<td>TRUE</td>': print '<td>FALSE</td>';
print "</tr>\n";
}
fclose($fp);
?>
</table>