PHP函数库分类十五.docx
《PHP函数库分类十五.docx》由会员分享,可在线阅读,更多相关《PHP函数库分类十五.docx(21页珍藏版)》请在冰豆网上搜索。
![PHP函数库分类十五.docx](https://file1.bdocx.com/fileroot1/2023-7/15/c7064b21-7488-4dd1-9f5e-3b582d934297/c7064b21-7488-4dd1-9f5e-3b582d9342971.gif)
PHP函数库分类十五
PHP函数库分类十五
5.PHP错误处理
·debug_backtrace -Generatesabacktrace
debug_backtrace
(PHP4>=4.3.0,PHP5)
debug_backtrace — Generatesabacktrace
说明
array debug_backtrace ([ int $options =DEBUG_BACKTRACE_PROVIDE_OBJECT[, int $limit =0 ]])
debug_backtrace() generatesaPHPbacktrace.
参数
options
Asof5.3.6,thisparameterisabitmaskforthefollowingoptions:
debug_backtrace() options
DEBUG_BACKTRACE_PROVIDE_OBJECT
Whetherornottopopulatethe"object"index.
DEBUG_BACKTRACE_IGNORE_ARGS
Whetherornottoomitthe"args"index,andthusallthefunction/methodarguments,tosavememory.
Before5.3.6,theonlyvaluesrecognizedare TRUE or FALSE,whicharethesameassettingornotsettingthe DEBUG_BACKTRACE_PROVIDE_OBJECT optionrespectively.
limit
Asof5.4.0,thisparametercanbeusedtolimitthenumberofstackframesreturned.Bydefault(limit=0)itreturnsallstackframes.
返回值
Returnsanarrayofassociative arrays.Thepossiblereturnedelementsareasfollows:
Possiblereturnedelementsfrom debug_backtrace()
名字
类型
说明
function
string
Thecurrentfunctionname.Seealso __FUNCTION__.
line
integer
Thecurrentlinenumber.Seealso __LINE__.
file
string
Thecurrentfilename.Seealso __FILE__.
class
string
Thecurrent class name.Seealso __CLASS__
object
object
Thecurrent object.
type
string
Thecurrentcalltype.Ifamethodcall,"->"isreturned.Ifastaticmethodcall,":
:
"isreturned.Ifafunctioncall,nothingisreturned.
args
array
Ifinsideafunction,thisliststhefunctionsarguments.Ifinsideanincludedfile,thisliststheincludedfilename(s).
更新日志
版本
说明
5.4.0
Addedtheoptionalparameter limit.
5.3.6
Theparameter provide_object changedto options andadditionaloptionDEBUG_BACKTRACE_IGNORE_ARGS isadded.
5.2.5
Addedtheoptionalparameter provide_object.
5.1.1
Addedthecurrent object asapossiblereturnelement.
范例
Example#1 debug_backtrace() example
php
// filename:
/tmp/a.php
function a_test($str)
{
echo "Hi:
$str";
var_dump(debug_backtrace());
}
a_test('friend');
?
>
php
// filename:
/tmp/b.php
include_once '/tmp/a.php';
?
>
Resultssimilartothefollowingwhenexecuting /tmp/b.php:
Hi:
friend
array
(2){
[0]=>
array(4){
["file"]=>string(10)"/tmp/a.php"
["line"]=>int(10)
["function"]=>string(6)"a_test"
["args"]=>
array
(1){
[0]=>&string(6)"friend"
}
}
[1]=>
array(4){
["file"]=>string(10)"/tmp/b.php"
["line"]=>int
(2)
["args"]=>
array
(1){
[0]=>string(10)"/tmp/a.php"
}
["function"]=>string(12)"include_once"
}
}
·debug_print_backtrace -Printsabacktrace
debug_print_backtrace
(PHP5)
debug_print_backtrace — Printsabacktrace
说明
void debug_print_backtrace ([ int $options =0 [, int $limit =0 ]])
debug_print_backtrace() printsaPHPbacktrace.Itprintsthefunctioncalls,included/requiredfilesand eval()edstuff.
参数
options
Asof5.3.6,thisparameterisabitmaskforthefollowingoptions:
debug_print_backtrace() options
DEBUG_BACKTRACE_IGNORE_ARGS
Whetherornottoomitthe"args"index,andthusallthefunction/methodarguments,tosavememory.
limit
Asof5.4.0,thisparametercanbeusedtolimitthenumberofstackframesprinted.Bydefault(limit=0)itprintsallstackframes.
返回值
没有返回值。
更新日志
版本
说明
5.4.0
Addedtheoptionalparameter limit.
5.3.6
Addedtheoptionalparameter options.
范例
Example#1 debug_print_backtrace() example
php
// include.php file
function a() {
b();
}
function b() {
c();
}
function c(){
debug_print_backtrace();
}
a();
?
>
php
// test.php file
// this is the file you should run
include 'include.php';
?
>
以上例程的输出类似于:
#0c()calledat[/tmp/include.php:
10]
#1b()calledat[/tmp/include.php:
6]
#2a()calledat[/tmp/include.php:
17]
#3include(/tmp/include.php)calledat[/tmp/test.php:
3]
·error_get_last -Getthelastoccurrederror
error_get_last
(PHP5>=5.2.0)
error_get_last — Getthelastoccurrederror
说明
array error_get_last ( void )
Getsinformationaboutthelasterrorthatoccurred.
返回值
Returnsanassociativearraydescribingthelasterrorwithkeys"type","message","file"and"line".IftheerrorhasbeencausedbyaPHPinternalfunctionthenthe"message"beginswithitsname.Returns NULL iftherehasn'tbeenanerroryet.
范例
Example#1An error_get_last() example
php
echo $a;
print_r(error_get_last());
?
>
以上例程的输出类似于:
Array
(
[type]=>8
[message]=>Undefinedvariable:
a
[file]=>C:
WWWindex.php
[line]=>2
)
·error_log -Sendanerrormessagesomewhere
error_log
(PHP4,PHP5)
error_log — Sendanerrormessagesomewhere
说明
bool error_log ( string $message [, int $message_type =0 [, string $destination[, string $extra_headers ]]])
Sendsanerrormessagetothewebserver'serrorlogortoafile.
参数
message
Theerrormessagethatshouldbelogged.
message_type
Sayswheretheerrorshouldgo.Thepossiblemessagetypesareasfollows:
error_log() logtypes
0
message issenttoPHP'ssystemlogger,usingtheOperatingSystem'ssystemloggingmechanismorafile,dependingonwhattheerror_logconfigurationdirectiveissetto.Thisisthedefaultoption.
1
message issentbyemailtotheaddressinthe destinationparameter.Thisistheonlymessagetypewherethefourthparameter, extra_headers isused.
2
Nolongeranoption.
3
message isappendedtothefile destination.Anewlineisnotautomaticallyaddedtotheendofthe message string.
4
message issentdirectlytotheSAPIlogginghandler.
destination
Thedestination.Itsmeaningdependsonthe message_type parameterasdescribedabove.
extra_headers
Theextraheaders.It'susedwhenthe message_type parameterissetto 1.Thismessagetypeusesthesameinternalfunctionas mail() does.
返回值
成功时返回 TRUE,或者在失败时返回 FALSE.
更新日志
版本
说明
5.2.7
Thepossiblevalueof4wasaddedto message_type.
范例
Example#1 error_log() examples
php
// Send notification through the server log if we can not
// connect to the database.
if (!
Ora_Logon($username, $password)) {
error_log("Oracle database not available!
", 0);
}
// Notify administrator by email if we run out of FOO
if (!
($foo = allocate_new_foo())) {
error_log("Big trouble, we're all out of FOOs!
", 1,
"operator@");
}
// another way to call error_log():
error_log("You messed up!
", 3, "/var/tmp/my-errors.log");
?
>
·error_reporting -SetswhichPHPerrorsarereported
error_reporting
(PHP4,PHP5)
error_reporting — SetswhichPHPerrorsarereported
说明
int error_reporting ([ int $level ])
The error_reporting() functionsetstheerror_reportingdirectiveatruntime.PHPhasmanylevelsoferrors,usingthisfunctionsetsthatlevelfortheduration(runtime)ofyourscript.Iftheoptional level isnotset, error_reporting() willjustreturnthecurrenterrorreportinglevel.
参数
level
Thenewerror_reportinglevel.Ittakesoneitherabitmask,ornamedconstants.Usingnamedconstantsisstronglyencouragedtoensurecompatibilityforfutureversions.Aserrorlevelsareadded,therangeofintegersincreases,soolderinteger-basederrorlevelswillnotalwaysbehaveasexpected.
Theavailableerrorlevelconstantsandtheactualmeaningsoftheseerrorlevelsaredescribedinthepredefinedconstants.
返回值
Returnstheolderror_reportinglevelorthecurrentlevelifno level parameterisgiven.
更新日志
版本
说明
5.4.0
E_STRICT becamepartof E_ALL.
5.3.0
E_DEPRECATED and E_USER_DEPRECATED introduced.
5.2.0
E_RECOVERABLE_ERROR introduced.
5.0.0
E_STRICT introduced(notpartof E_ALL).
范例
Example#1 error_reporting() examples
php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
?
>
注释
Warning
Mostof E_STRICT errorsareevaluatedatthecompiletimethussucherrorsarenotreportedinthefilewhereerror_reportingisenhancedtoinclude E_STRICTerrors(andviceversa).
Tip
Passinginthevalue -1 willshoweverypossibleerror,evenwhennewlevelsandconstantsareaddedinfuturePHPversions.The E_ALL constantalsobehavesthiswayasofPHP5.4.
·restore_error_handler -Restoresthepreviouserrorhandlerfunction
restore_error_handler
(PHP4>=4.0.1,PHP5)
restore_error_handler — Restoresthepreviouserrorhandlerfunction
说明
bool restore_error_handler ( void )
Usedafterchangingtheerrorhandlerfunctionusing set_error_handler(),toreverttothepreviouserrorhandler(whichcouldbethebuilt-inorauserdefinedfunction).
返回值
Thisfunctionalwaysreturns TRUE.
范例
Example#1 restore_error_handler() example
Decideif unserialize() causedanerror,thenrestoretheoriginalerrorhandler.
php
function unserialize_handler($errno, $errstr)
{
echo "Invalid serialized value.";
}
$serialized = 'foo';
set_error_handler('unserialize_handler');
$original = unserialize($serialized);
restore_error_handler();
?
>
以上例程会输出:
Invalidserializedvalue.
注释
Note:
Calling restore_error_handler() fromthe error_handler functionisignored.
·restore_exception_handler -Restoresthepreviouslydefinedexceptionhandlerfunction
restore_exception_handler
(PHP5)
restore_exception_handler — Restoresthe