PHP の error_reporting()
PHP にはさまざまなレベルのエラーがありますが、error_reporting は、報告されるエラーの内容を示し、実行時に error_reporting ディレクティブを決定する PHP の関数です。この関数を使用すると、スクリプトの必要な期間 (通常は実行時間) に対して規定のレベルを設定できます。指定された入力に基づいて古いエラー レポート レベルを返します。パラメータが指定されていない場合は現在のレポート レベルを返します。
無料ソフトウェア開発コースを始めましょう
Web 開発、プログラミング言語、ソフトウェア テスト、その他
パラメータを含む構文
パラメータを含む構文は次のとおりです:
構文:
error_reporting(level)
パラメータ:
オプションであり、その入力関数が受け取るパラメーター レベルは 1 つだけです。現在のスクリプトのエラー報告レベルを指定します。受け入れられる値は定数名と値番号です。
注: PHP の将来のバージョンとの互換性を確保するには、名前付き定数を使用することをお勧めします。事前定義された定数がいくつかあり、その説明は次のとおりです。
1. E_Error: これらは、回復できない致命的なランタイム エラーを示し、スクリプトの実行は停止されます。
2. E_警告: これらは致命的ではないエラーであり、スクリプトの実行は続行されます。
3. E_Parse: これは、パーサーによってのみ生成されるコンパイル時の解析エラーを示します。
4. E_ Notice: これは、スクリプトがエラーを示す何かを見つけたことを示す実行時通知を発行しますが、通常のスクリプトの実行中にも発生する可能性があります。
5. E_Core_Error: PHP の初期起動中に、PHP のコアによって生成されるいくつかの致命的なエラーが発生する可能性があります。
6. E_Core_Warning: これは、PHP の初期起動中に発生し、PHP のコアによっても生成される致命的ではないエラーを示します。
7. E_Compile_Error: コンパイル時に発生する致命的なエラーを表示します。これらは、Zend スクリプト エンジンによって生成されます。
8. E_Compile_Warning: 上記と同様に、これらはコンパイル時警告を表示するか、致命的ではないエラーと呼ばれることもあり、Zend スクリプト エンジンによっても生成されます。
9. E_User_Error: ユーザーによって生成されたエラーが表示されます。これは、PHP コードで PHP 関数を使用して生成される点を除けば E_ERROR に似ています。
10. E_All: これは、E_STRICT を除くすべてのエラーと警告をサポートする上記すべての組み合わせのようなものです。
戻り値:
error_reporting 関数は、パラメータが指定されていない場合は、古いレポート レベルまたは現在のエラー レポート レベルを返します。
PHP での error_reporting の仕組み
この関数を使用すると、開発者はさまざまな種類のエラーと、アプリケーションでスローされるそのようなエラーの数を実際に制御できます。この関数は、PHP ini 設定ファイルに存在する error_reporting ディレクティブを設定します。
error_reporting(0);
- When 0 is passed to the error reporting function it removes all warnings, errors, parse related messages and notices, if any. Instead of having to include this line in each of the PHP code files, it is practical to have it added and to turn off these report messages in the ini file present or in the .htaccess.
error_reporting(E_NOTICE);
- In PHP the variables can be used even when not declared. But this practice is not feasible as the undeclared variables may cause application related issues if it is used in conditional statements and loops. This may also take place because of the spelling mismatch between the declared variables and of that being used for conditions and loops. When this E_NOTICE will be passed into the error_reporting function, only then these undeclared variables will be shown in the web application.
error_reporting(E_ALL & ~E_NOTICE);
- This error reporting function helps to filter out the errors which can be displayed. The “~” character here means the “not/no” and hence ~E_NOTICE here means to not show any notices. Here the “&” character represents “true for all” whereas “|” means as long as one of the parameters is true. They are exactly similar to the functions AND and OR in PHP.
error_reporting(E_ALL); error_reporting(-1); ini_set('error_reporting', E_ALL);
- All of the above lines serve the same purpose i.e. show all the errors. E_ALL is the most widely used function among all others by developers to display error messages as it is more comprehensible and intelligible.
Error Logging in PHP using error_log() Function
It happens so that during the production phase, error messages are to be hidden from the end-users but this information is needed to be registered for tracing purpose. And the best way to record these errors on the production web application is to write and store in log files.
An easy way to log these is by using the error_log function which takes our parameters as input. The only mandatory parameter here is the first one which contains details about the errors and what all to be logged. Other parameters like the type, destination, and header are non-mandatory here for this function.
error_log("Error found!", 0);
- The type parameter will be set to 0 by default if not given, and the log information will be appended at the end of the log file generated in the webserver.
error_log("Error information being emailed!", 1, "[email protected]");
- The type parameter here is 1 will email this log specified in the 3rd parameter which is the email id. For this to work, the PHP ini file must be having a correct SMTP configuration to send out emails. Some of the parameters required for these include host, encryption type, port, password and username.
error_log("Write errors to this file", 3, "https://cdn.educba.com/tmp/errorfile.log")<em>;</em>
- The same error logs can also be written down to the required file whose path will be given in the third parameter. Make sure the given path has all required permissions.
Example of error_reporting() in PHP
Given below is the example:
Code:
<?php $a = 1; trigger_error("user warning!", E_USER_WARNING); $a = 2; echo "Value of $a is ${$a}"; error_reporting(0); error_reporting(E_ALL); ?>
Output:
Advantages of using error_reporting function in PHP
- error_reporting is good for debugging purposes and for developing web application.
- Each and every error can be logged and fixed as soon as it happens using this function.
- To not show it to the end-user, make sure you redirect the errors to a log file while releasing it.
Conclusion
Hence we can say that error_reporting() function in PHP are therefore helpful in cases when there are a lot of problems with the PHP web application and we need to display all of these errors and warnings either for development or debugging purposes. It is a function we can enable different kinds of warnings or error messages and most of them are as discussed above.
以上がPHP の error_reporting()の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。

PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7

PHPとPythonにはそれぞれ独自の利点があり、プロジェクトの要件に従って選択します。 1.PHPは、特にWebサイトの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンス、機械学習、人工知能に適しており、簡潔な構文を備えており、初心者に適しています。

PHPは、電子商取引、コンテンツ管理システム、API開発で広く使用されています。 1)eコマース:ショッピングカート機能と支払い処理に使用。 2)コンテンツ管理システム:動的コンテンツの生成とユーザー管理に使用されます。 3)API開発:RESTFUL API開発とAPIセキュリティに使用されます。パフォーマンスの最適化とベストプラクティスを通じて、PHPアプリケーションの効率と保守性が向上します。

PHPは依然として動的であり、現代のプログラミングの分野で重要な位置を占めています。 1)PHPのシンプルさと強力なコミュニティサポートにより、Web開発で広く使用されています。 2)その柔軟性と安定性により、Webフォーム、データベース操作、ファイル処理の処理において顕著になります。 3)PHPは、初心者や経験豊富な開発者に適した、常に進化し、最適化しています。

PHPは主に手順プログラミングですが、オブジェクト指向プログラミング(OOP)もサポートしています。 Pythonは、OOP、機能、手続き上のプログラミングなど、さまざまなパラダイムをサポートしています。 PHPはWeb開発に適しており、Pythonはデータ分析や機械学習などのさまざまなアプリケーションに適しています。

PHPとPythonには独自の利点と短所があり、選択はプロジェクトのニーズと個人的な好みに依存します。 1.PHPは、大規模なWebアプリケーションの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンスと機械学習の分野を支配しています。

PHPは、特に迅速な開発や動的なコンテンツの処理に適していますが、データサイエンスとエンタープライズレベルのアプリケーションには良くありません。 Pythonと比較して、PHPはWeb開発においてより多くの利点がありますが、データサイエンスの分野ではPythonほど良くありません。 Javaと比較して、PHPはエンタープライズレベルのアプリケーションでより悪化しますが、Web開発により柔軟性があります。 JavaScriptと比較して、PHPはバックエンド開発により簡潔ですが、フロントエンド開発のJavaScriptほど良くありません。
