[ Pobierz całość w formacie PDF ]
.If you want to set a default level,you can do so in php.ini by assigning a number to the error_reporting option.Fortunately, error reporting numbers are stored in constant variables, which makesit easy to choose the level you require.Table 22.2 lists the different levels that youcan apply.Table 22.2: Levels of Strictness for Error ReportingVariable Name Description ActionE_ALL All Every kind of Dependenterror on errorencountered 408E_ERROR Errors Fatal errors Notify and(memory end scriptallocation executionproblems,for example)E_WARNING Warnings Nonfatal Notify oferrors (such problem butas do not endmalformed executionfunctionarguments)E_PARSE Parser Parser does Notify anderrors not end scriptunderstand executionsyntax.E_NOTICE Notices Potential Notify andproblem continue(uninitializedvariables, forexample)E_CORE_ERROR Start-up Fatal errors Aborterrors encountered start-upduringstart-upE_CORE_WARNING Start-up Nonfatal Notify andwarnings errors continueencounteredduringstart-upYou cannot use these settings to change the action taken when an error is found;you only have control over whether the error is reported to the browser.You are likely to want to combine two or more of these error reporting levels.Youcan do this by separating the constants you would like to apply with a pipe (|)symbol.For example,error_reporting = E_ERROR|E_WARNINGwill set the error reporting to include both errors and warnings.If you wish to include all error types, you can use E_ALL.What would you do if you 409would like to report all error types apart from one?error_reporting = E_ALL & ~E_NOTICEwill set error reporting to all errors apart from notices.E_ERROR | E_WARNING andE_ALL & ~E_NOTICE are binary arithmetic expressions that produce a new numberthat defines the level of error reporting.Binary arithmetic is beyond the scope of thisbook, but the procedure should be clear enough.You can override the error_reporting option in php.ini with a function unsurprisinglycalled error_reporting().This requires an integer representing the level of errorreporting you want set for the execution of your script, and returns the previouserror reporting setting.Once again you can use the constants that PHP provides foryou.Let's look at an example where changing the level of error reporting can helpyou.See if you can spot the deliberate mistake in Listing 22.3Listing 22.3: A Deliberate Error1:As you can see, we are testing the $flag variable, but we have misspelled it.Thereis no fatal error in this script, so it will always run.At an error reporting level ofE_ERROR | E_WARNING | E_PARSE (which resolves to 7), there will be no warning,and your code might find its way into a production environment, where it will waituntil the worst possible moment to manifest itself.If we change the value passed toerror_reporting() to E_ERROR | E_WARNING | E_PARSE | E_NOTICE (whichresolves to 15 and includes notices), however, this script will generate the followingoutput:Warning: Undefined variable: flg in/home/matt/htdocs/php-book/debug/listing22.3.php on line 4I know that $flag is NOT 45Not only do we now know that there is a problem, we also know the line number atwhich it was found.We could achieve the same effect by setting the error reportinglevel to E_ALL. 410Setting the error reporting level to include notices can have less fortunate sideeffects, though.You might occasionally want to work with an undefined variable.Consider the following code fragment:We have taken advantage of the fact that printing an undefined variable has asimilar effect to printing an empty string (no effect at all).The "user" field in thepreceding form will include a previously submitted value for $user, or nothing at all.If we were to call error_reporting() with an argument of 15, we would get an errormessage.Writing Error Messages to a Log FileThe bugs that we have been chasing so far are immediate problems for the mostpart.In other words, they are caught as you write your code.Some bugs, though,occur later on, perhaps as a result in changes to a script's environment.Imagine ascript that writes data to a text file when a user submits a form.You test your script,monitor its performance for a while in a production environment, and then leave itto get on with its job.A few weeks later, you mistakenly remove the directory thatcontains the file to which the script is programmed to write.The error caused by thiscould go unrecorded.PHP provides error_log(), a built-in method of logging errors to the server's error logor to a file of your choosing [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • andsol.htw.pl