PHPは、fwriteを使用してファイルに書き込む前に、引用符を自動的にエスケープします。テストコードページを作成しようとしています。これが私が持っているコードです:
<?php
if ($_GET['test'] == 'true') {
$code = $_POST['code'];
$file = fopen('testcode.inc.php', 'w+');
fwrite($file, $code);
fclose($file);
require_once('testcode.inc.php');
}
else {
echo "
<form method='post' action='testcode.php?test=true'>
<textarea name='code' id='code'></textarea><br><br>
<button type='submit'>Test!</button><br>
</form>
";
}
?>
フォームに次のように入力すると、次のようになります。
<?php
echo 'test';
?>
次のようにファイルに保存されます。
<?php
echo \'test\';
?>
PHPが自動的に引用符をエスケープするのはなぜですか?