教程中的 PHP 示例不会一直运行


PHP example from tutorial won't run all the way through

>我在它停止运行之前和之后放置了一个echo。文件file1.txt已写入,但随后不返回最终echo确认。

<?php
$fh = fopen("file1.txt", 'w') or die("Failed to create file");
$text = <<<_END
Line 1
Line 2
Line 3
_END;
fwrite($fh, $text) or die("Could not write to file");
echo "This shows up in my browser";
$fclose($fh);
echo "This doesn't";
?>

您的脚本有错误

取代

 $fclose($fh);

 fclose($fh);

要查看所有错误,请将其添加到页面顶部

error_reporting(E_ALL);
ini_set('display_errors','On');

由于您正在学习是否想使用可变函数,这将起作用

$fclose = "fclose";
$fclose ($fh);

最后,使用file_put_contents http://php.net/manual/en/function.file-put-contents.php 进行学习的更简单版本的代码

$text = <<<_END
Line 1
Line 2
Line 3
_END;
file_put_contents ("file1.txt", $text );
echo "This shows up in my browser";

$fclose($fh);应该fclose($fh); 。这是一个函数,而不是变量。