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>