炊きたてのご飯が食べたい

定時に帰れるっていいね。自宅勤務できるっていいね。子どもと炊きたてのご飯が食べられる。アクトインディでは積極的にエンジニアを募集中です。

(php) header()関数でエラー [ Warning: Cannot modify header information - headers already sent by … ]


phpでサイトを作っていてエラー。

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test.php:3) in C:\xampp\htdocs\test.php on line 5

header()関数よりも前の行で、出力を行ってはダメだよと怒られた。
・test.php

var_dump($_SESSION);
header("Location: test.php");

header("Location: home.php") より前に var_dump で出力していたのがエラーの原因。
test2.php

header("Location: test2.php");
var_dump($_SESSION);

は問題なく通る。
header() 関数は、すべての出力の前にコールする必要があり、include() または require() 関数に 空白または空行が含まれていると、header() の前に出力が行われてしまいエラーになるケースが多いみたい。気をつけよう。