0

開始タグと終了<?php /* stuff */ ?>タグがあり、その後に標準の HTML がある PHP スクリプトがある場合、たとえば、スクリプトがエラーをキャッチした場合に、サーバーに通常の HTML の送信を停止するように指示することは可能ですか?

例...

<?php
// ... rest of the script above here

$buildout = '(compiler output will display here)';
$execsout = '(program output will display here)';

// ... errors would be displayed here using die();
?>
<!doctype html>
<head>
<title>Test Page</title>
<meta charset="utf-8"/>
<!-- rest of the HTML below here -->

必要なことは、スクリプトがまだ PHP 部分を処理している間に die() し、HTML を送信しないことです。その理由は、PHP 自体を使用してエコー/印刷するには HTML が多すぎて、編集がはるかに面倒になるからです。

4

4 に答える 4

2

php ステータス コードを応答ヘッダーとして使用してみてください():

if ($error)
{
header('HTTP/1.1 500 Internal Server Error');
 exit();
}

or 

header('HTTP/1.1 500 Internal Server Error');
exit();
于 2013-09-26T21:53:38.193 に答える
1

エラー ハンドラーに例外をスローさせることで、すべてのエラーを致命的にすることができます。

<?php
/**
 * throw exceptions based on E_* error types
 */
set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context)
{
    // error was suppressed with the @-operator
    if (0 === error_reporting()) { return false;}
    throw new ErrorException("A $err_severity had occurred: $err_msg");
});

これにより、警告や通知を含むすべてが致命的になり、スクリプトが完全に停止することに注意してください。

于 2013-09-26T21:35:48.060 に答える
1

次のようなものを試すことができます

if (!$error):
?>
    <!--- HTML here --->
<?php
endif;
?>
于 2013-09-26T21:30:04.653 に答える