重要なファイルに対して、require の代わりに include を使用している php CMS が表示されます。header.php などのこのファイルは必須ではありませんか? ファイルを開くことができない場合、スクリプトは続行します。
4 に答える
Depends. Could be the sign of laziness or uneducated developer. But if the header is an important file—i.e. contains authorization logic (which it shouldn't)—then yes, you would want to use require
in place of include
.
include は警告の発生を許可するため、残りのスクリプトは引き続き実行されます。require によって致命的なエラーが発生し、すぐに実行が停止します。これは通常、エラーの説明が出力された完全に空白のページになります。関数を複数回定義すると致命的なエラーが出力されるため、特にスクリプト内のスクリプト内にスクリプトがある場合は、require_once が最も便利だと思います。個人的にはrequire_once()の方が好きです
ここにいくつかの例があります
条件: ファイル test.php が存在しない
include() の使用:
<?php include("test.php"); print "<p>おい、この文字列はまだ印刷されてるぞ!</p>"; ?>
結果:
警告: include(test.php) [function.include]: 開けませんでした stream: そのようなファイルやディレクトリはありません /home/webspotm/public_html/error/index.php 2行目 警告: include() [function.include]: 'test.php' を開けませんでした 含めるため (include_path='.:/home/webspotm/.imports') /home/webspotm/public_html/error/index.php の 2 行目 Hey This String Still Prints Dude!
require() の使用:
<?php require("test.php"); print "<p>はい、試すことさえしないでください。このコードは表示されません!</p>"; ?>
結果:
警告: require(test.php) [function.require]: ストリームを開けませんでした: そのようなファイルまたはディレクトリはありません /home/webspotm/public_html/error/index.php 2行目 致命的なエラー: require() [function.require]: 開けませんでした 'test.php' (include_path='.:/home/webspotm/.imports') が必要 /home/webspotm/public_html/error/index.php 2行目
作成者が include と require の違いを知らなかったか、ファイルが含まれなくても気にしないかのどちらかです。
include() コンストラクトは、ファイルが見つからない場合に警告を発します。これは、致命的なエラーを発生させる require() とは異なる動作です。