PHP 第六周函数学习记录
timezone_identifiers_list()
作用
返回带有所有时区标识符的数值数组。
用法
timezone_identifiers_list(what,country);
what 可选。规定一个 DateTimeZone 类常量
1 = AFRICA
2 = AMERICA
4 = ANTARCTICA
8 = ARCTIC
16 = ASIA
32 = ATLANTIC
64 = AUSTRALIA
128 = EUROPE
256 = INDIAN
512 = PACIFIC
1024 = UTC
2047 = ALL
4095 = ALL_WITH_BC
4096 = PER_COUNTRY
测试案例
print_r(timezone_identifiers_list(1));
结果
Array
(
[0] => Africa/Abidjan
[1] => Africa/Accra
[2] => Africa/Addis_Ababa
[3] => Africa/Algiers
[4] => Africa/Asmara
[5] => Africa/Bamako
[6] => Africa/Bangui
[7] => Africa/Banjul
[8] => Africa/Bissau
[9] => Africa/Blantyre
[10] => Africa/Brazzaville
[11] => Africa/Bujumbura
[12] => Africa/Cairo
[13] => Africa/Casablanca
[14] => Africa/Ceuta
[15] => Africa/Conakry
[16] => Africa/Dakar
[17] => Africa/Dar_es_Salaam
[18] => Africa/Djibouti
[19] => Africa/Douala
[20] => Africa/El_Aaiun
[21] => Africa/Freetown
[22] => Africa/Gaborone
[23] => Africa/Harare
[24] => Africa/Johannesburg
[25] => Africa/Juba
[26] => Africa/Kampala
[27] => Africa/Khartoum
[28] => Africa/Kigali
[29] => Africa/Kinshasa
[30] => Africa/Lagos
[31] => Africa/Libreville
[32] => Africa/Lome
[33] => Africa/Luanda
[34] => Africa/Lubumbashi
[35] => Africa/Lusaka
[36] => Africa/Malabo
[37] => Africa/Maputo
[38] => Africa/Maseru
[39] => Africa/Mbabane
[40] => Africa/Mogadishu
[41] => Africa/Monrovia
[42] => Africa/Nairobi
[43] => Africa/Ndjamena
[44] => Africa/Niamey
[45] => Africa/Nouakchott
[46] => Africa/Ouagadougou
[47] => Africa/Porto-Novo
[48] => Africa/Sao_Tome
[49] => Africa/Tripoli
[50] => Africa/Tunis
[51] => Africa/Windhoek
)
timezone_location_get()
作用
返回指定时区的位置信息
用法
timezone_location_get(object);
测试案例
$tz=timezone_open("Asia/Taipei");
print_r(timezone_location_get($tz));
结果
Array (
[country_code] => TW
[latitude] => 25.05
[longitude] => 121.5
[comments] =>
)
timezone_name_from_abbr()
作用
根据时区缩略语返回时区名称
用法
timezone_name_from_abbr(abbr,gmtoffset,isdst);
abbr 必需。规定时区缩略语。
gmtoffset 可选。规定相对于 GMT 的以秒为单位的偏移量。默认为 -1,表示返回第一个被找到的匹配缩略语的时区。否则搜索精确的偏移量。如果没有找到,则返回任意偏移量的第一个时区。
isdst 可选。规定夏令时指示器。
-1 = 默认。搜索时是否考虑时区的夏令时
1 = 表示 gmtoffset 是受夏令时影响的一个偏移量
0 = 表示 gmtoffset 是不受夏令时影响的一个偏移量
测试案例
echo timezone_name_from_abbr("EST") . "<br>";
echo timezone_name_from_abbr("",7200,0);
结果
America/New_York
Europe/Helsinki
timezone_name_get()
作用
返回时区的名称
用法
timezone_name_get(object);
object 必需。规定一个 DateTimeZone 对象。
测试案例
$tz=timezone_open("Europe/Paris");
echo timezone_name_get($tz);
结果
Europe/Paris
timezone_offset_get()
作用
返回相对于 GMT 的时区偏移
用法
timezone_offset_get(object,datetime);
测试案例
$tz=timezone_open("Asia/Taipei");
$dateTimeOslo=date_create("now",timezone_open("Europe/Oslo"));
echo timezone_offset_get($tz,$dateTimeOslo);
结果
28800
timezone_open()
作用
创建一个新的 DateTimeZone 对象
用法
timezone_open(timezone);
测试案例
$tz=timezone_open("Europe/Paris");
echo timezone_name_get($tz);
结果
Europe/Paris
timezone_version_get()
作用
函数返回时区数据库的版本。
用法
timezone_open(timezone);
测试案例
echo timezone_version_get();
结果
2012.2
chdir()
作用
函数改变当前的目录。
用法
chdir(directory);
测试案例
// Get current directory
echo getcwd() . "<br>";
// Change directory
chdir("images");
// Get current directory
echo getcwd();
结果
/home/php
/home/php/images
chroot()
作用
函数改变当前进程的根目录为 directory,并把当前工作目录改为 "/"。
注意:该函数需要 root 权限,且仅在 GNU 和 BSD 系统上仅当使用 CLI、CGI、嵌入式 SAPI 时可用。该函数没有在 Windows 平台上实现。
用法
chroot(directory);
测试案例
// Change root directory
chroot("/path/to/chroot/");
// Get current directory
echo getcwd();
结果
/
closedir()
作用
函数关闭目录句柄
用法
closedir(dir_handle);
测试案例
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
结果
filename: cat.gif
filename: dog.gif
filename: horse.gif
dir()
作用
dir() 函数返回 Directory 类的实例。该函数用于读取一个目录,包含如下:
给定的要打开的目录
dir() 的 handle 和 path 两个属性是可用的
handle 和 path 属性有三个方法:read()、rewind() 和 close()
用法
dir(directory,context);
测试案例
$d = dir(getcwd());
echo "Handle: " . $d->handle . "<br>";
echo "Path: " . $d->path . "<br>";
while (($file = $d->read()) !== false){
echo "filename: " . $file . "<br>";
}
$d->close();
结果
Handle: Resource id #2
Path: /etc/php
filename: .
filename: ..
filename: ajax.gif
filename: books.xml
filename: cdcatalog.xml
filename: cd_catalog.xml
filename: default.html
filename: demo_array.html
filename: demo_array.htm
...
...
...
getcwd()
作用
函数返回当前工作目录。
用法
getcwd();
测试案例
echo getcwd()
结果
/home/php
opendir()
作用
函数打开目录句柄。
用法
opendir(path,context);
测试案例
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
结果
filename: cat.gif
filename: dog.gif
filename: horse.gif
readdir()
作用
函数返回目录中下一个文件的文件名。
用法
readdir(dir_handle);
测试案例
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
结果
filename: cat.gif
filename: dog.gif
filename: horse.gif
rewinddir()
作用
函数重置由 opendir() 创建的目录句柄。
用法
rewinddir(dir_handle);
测试案例
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
// List files in images directory
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
rewinddir();
// List once again files in images directory
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
结果
filename: cat.gif
filename: dog.gif
filename: horse.gif
filename: cat.gif
filename: dog.gif
filename: horse.gif
scandir()
作用
函数返回指定目录中的文件和目录的数组。
用法
scandir(directory,sorting_order,context);
directory 必需。规定要扫描的目录。
sorting_order 可选。规定排列顺序。默认是 0,表示按字母升序排列。如果设置为 SCANDIR_SORT_DESCENDING 或者 1,则表示按字母降序排列。如果设置为 SCANDIR_SORT_NONE,则返回未排列的结果。
context 可选。规定目录句柄的环境。context 是可修改目录流的行为的一套选项。
测试案例
$dir = "/images/";
// Sort in ascending order - this is default
$a = scandir($dir);
// Sort in descending order
$b = scandir($dir,1);
print_r($a);
print_r($b);
结果
Array
(
[0] => .
[1] => ..
[2] => cat.gif
[3] => dog.gif
[4] => horse.gif
[5] => myimages
)
Array
(
[0] => myimages
[1] => horse.gif
[2] => dog.gif
[3] => cat.gif
[4] => ..
[5] => .
)
debug_backtrace()
作用
debug_backtrace() 函数生成 backtrace。
该函数显示由 debug_backtrace() 函数代码生成的数据。
返回一个关联数组。下面是可能返回的元素:
名称 | 类型 | 描述 |
---|---|---|
function | string | 当前的函数名。 |
line | integer | 当前的行号。 |
file | string | 当前的文件名。 |
class | string | 当前的类名。 |
object | object | 当前对象。 |
type | string | 当前的调用类型,可能的调用: 返回:”->” - 方法调用 返回:”::” - 静态方法调用 返回 nothing - 函数调用 |
args | array | 如果在函数中,列出函数参数。如果在被引用的文件中,列出被引用的文件名 |
用法
debug_backtrace()
测试案例
<?php
function one($str1, $str2)
{
two("Glenn", "Quagmire");
}
function two($str1, $str2)
{
three("Cleveland", "Brown");
}
function three($str1, $str2)
{
print_r(debug_backtrace());
}
one("Peter", "Griffin");
?>
结果
Array
(
[0] => Array
(
[file] => C:webfoldertest.php
[line] => 7
[function] => three
[args] => Array
(
[0] => Cleveland
[1] => Brown
)
)
[1] => Array
(
[file] => C:webfoldertest.php
[line] => 3
[function] => two
[args] => Array
(
[0] => Glenn
[1] => Quagmire
)
)
[2] => Array
(
[file] => C:webfoldertest.php
[line] => 14
[function] => one
[args] => Array
(
[0] => Peter
[1] => Griffin
)
)
)
debug_print_backtrace()
作用
debug_print_backtrace() 函数打印 backtrace。
该函数显示由 debug_print_backtrace() 函数代码生成的数据。
用法
debug_print_backtrace()
测试案例
<?php
function one($str1, $str2)
{
two("Glenn", "Quagmire");
}
function two($str1, $str2)
{
three("Cleveland", "Brown");
}
function three($str1, $str2)
{
debug_print_backtrace();
}
one("Peter", "Griffin");
?>
结果
#0 three(Cleveland, Brown) called at [C:webfoldertest.php:8]
#1 two(Glenn, Quagmire) called at [C:webfoldertest.php:4]
#2 one(Peter, Griffin) called at [C:webfoldertest.php:15]
error_get_last()
作用
error_get_last() 函数获得最后发生的错误。
该函数以数组的形式返回最后发生的错误。如果没有错误发生则返回 NULL。
返回的错误数组包含 4 个键名和键值:
- [type] - 错误类型
- [message] - 错误消息
- [file] - 发生错误所在的文件
- [line] - 发生错误所在的行
用法
error_get_last()
测试案例
<?php
echo $test;
print_r(error_get_last());
?>
结果
Array
(
[type] => 8
[message] => Undefined variable: test
[file] => C:webfoldertest.php
[line] => 2
)
error_log()
作用
error_log() 函数向服务器错误记录、文件或远程目标发送一个错误。
如果成功该函数返回 TRUE,如果失败该函数返回 FALSE。
返回的错误数组包含 4 个键名和键值:
- [type] - 错误类型
- [message] - 错误消息
- [file] - 发生错误所在的文件
- [line] - 发生错误所在的行
用法
error_log(error,type,destination,headers)
参数 | 描述 |
---|---|
error | 必需。要记录的错误消息。 |
type | 可选。规定错误记录的类型。 可能的记录类型:
|
destination | 可选。规定向何处发送错误消息。该参数的值依赖于 "type" 参数的值。 |
headers | 可选。只在 "type" 参数为 1 时使用。规定附加的头部,比如 From, Cc 以及 Bcc。附加头部由 CRLF (\r\n) 分隔。 注意:在发送电子邮件时,必须包含 From 头部。可以在 php.ini 文件中或者通过此参数设置。 |
测试案例
// 下面的实例发送一封带有自定义错误的电子邮件:
$test=2;
if ($test>1)
{
error_log("A custom error has been triggered",1,"someone@example.com","From: webmaster@example.com");
}
结果
// 上面代码接收到的邮件如下所示:
A custom error has been triggered
error_reporting()
作用
error_reporting() 函数规定报告哪个错误。
该函数设置当前脚本的错误报告级别。
该函数返回旧的错误报告级别。
用法
error_reporting(report_level)
report_level
值 | 常量 | 描述 |
---|---|---|
1 | E_ERROR | 运行时致命的错误。不能修复的错误。停止执行脚本。 |
2 | E_WARNING | 运行时非致命的错误。没有停止执行脚本。 |
4 | E_PARSE | 编译时的解析错误。解析错误应该只由解析器生成。 |
8 | E_NOTICE | 运行时的通知。脚本发现可能是一个错误,但也可能在正常运行脚本时发生。 |
16 | E_CORE_ERROR | PHP 启动时的致命错误。这就如同 PHP 核心的 E_ERROR。 |
32 | E_CORE_WARNING PHP | 启动时的非致命错误。这就如同 PHP 核心的 E_WARNING。 |
64 | E_COMPILE_ERROR | 编译时致命的错误。这就如同由 Zend 脚本引擎生成的 E_ERROR。 |
128 | E_COMPILE_WARNING | 编译时非致命的错误。这就如同由 Zend 脚本引擎生成的 E_WARNING。 |
256 | E_USER_ERROR | 用户生成的致命错误。这就如同由程序员使用 PHP 函数 trigger_error() 生成的 E_ERROR。 |
512 | E_USER_WARNING | 用户生成的非致命错误。这就如同由程序员使用 PHP 函数 trigger_error() 生成的 E_WARNING。 |
1024 | E_USER_NOTICE | 用户生成的通知。这就如同由程序员使用 PHP 函数 trigger_error() 生成的 E_NOTICE。 |
2048 | E_STRICT | 运行时的通知。PHP 建议您改变代码,以提高代码的互用性和兼容性。 |
4096 | E_RECOVERABLE_ERROR | 可捕获的致命错误。这就如同一个可以由用户定义的句柄捕获的 E_ERROR(见 set_error_handler())。 |
8191 | E_ALL | 所有的错误和警告的级别,除了 E_STRICT(自 PHP 6.0 起,E_STRICT 将作为 E_ALL的一部分)。 |
测试案例
//Disable error reporting
error_reporting(0);
//Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//Report all errors
error_reporting(E_ALL);
restore_error_handler()
作用
restore_error_handler() 函数恢复之前的错误处理程序。
该函数用于在通过 set_error_handler() 函数改变后恢复之前的错误处理程序。
该函数总是返回 TRUE。
用法
restore_error_handler()
测试案例
<?php
//custom error handler function
function customError($errno, $errstr, $errfile, $errline)
{
echo "<b>Custom error:</b> [$errno] $errstr<br />";
echo " Error on line $errline in $errfile<br />";
}
//set user-defined error handler
set_error_handler("customError");
$test=2;
//trigger error
if ($test>1)
{
trigger_error("A custom error has been triggered");
}
//restore built-in error handler
restore_error_handler();
//trigger error again
if ($test>1)
{
trigger_error("A custom error has been triggered");
}
?>
结果
Custom error: [1024] A custom error has been triggered
Error on line 14 in C:webfoldertest.php
Notice: A custom error has been triggered in
C:webfoldertest.php on line 21
restore_exception_handler()
作用
restore_exception_handler() 函数恢复之前的异常处理程序。
该函数用于在通过 set_exception_handler() 函数改变后恢复之前的异常处理程序。
该函数总是返回 TRUE。
用法
restore_exception_handler()
测试案例
<?php
restore_exception_handler();
throw new Exception('Uncaught Exception occured');
?>
结果
Fatal error: Uncaught exception 'Exception' with message
'Uncaught Exception occured' in C:webfoldertest.php:4
Stack trace: #0 {main} thrown in C:webfoldertest.php on line 4
set_error_handler()
作用
set_error_handler() 函数设置用户自定义的错误处理函数。
该函数用于创建运行期间的用户自己的错误处理方法。
该函数返回旧的错误处理程序,如果失败则返回 NULL。
用法
set_error_handler(error_function,error_types)
error_function 必需。规定发生错误时运行的函数。
error_types 可选。规定在哪个错误报告级别会显示用户定义的错误。默认是 "E_ALL"。
error_function(error_level, error_message, error_file, error_line, error_context)
参数 | 描述 |
---|---|
error_level | 必需的。规定用户自定义的错误的错误报告级别。必须是值数字。 |
error_message | 必需的。规定用户自定义的错误的错误消息。 |
error_file | 可选。规定发生错误的文件名。 |
error_line | 可选。规定发生错误的行号。 |
error_context | 可选。规定指向活跃符号表中发生错误的数组。换句话说,error_context 将包含一个说明每个变量引发错误的存在范围的数组。 |
错误报告级别
值 | 常量 | 描述 |
---|---|---|
2 | E_WARNING | 运行时非致命的错误。没有停止执行脚本。 |
8 | E_NOTICE | 运行时的通知。脚本发现可能是一个错误,但也可能在正常运行脚本时发生。 |
256 | E_USER_ERROR | 用户生成的致命错误。这就如同由程序员使用 PHP 函数 trigger_error() 生成的 E_ERROR。 |
512 | E_USER_WARNING | 用户生成的非致命错误。这就如同由程序员使用 PHP 函数 trigger_error() 生成的 E_WARNING。 |
1024 | E_USER_NOTICE | 用户生成的通知。这就如同由程序员使用 PHP 函数 trigger_error() 生成的 E_NOTICE。 |
4096 | E_RECOVERABLE_ERROR | 可捕获的致命错误。这就如同一个可以由用户定义的句柄捕获的 E_ERROR(见 set_error_handler())。 |
8191 | E_ALL | 所有的错误和警告的级别,除了 E_STRICT(自 PHP 6.0 起,E_STRICT 将作为 E_ALL的一部分)。 |
测试案例
<?php
//error handler function
function customError($errno, $errstr, $errfile, $errline)
{
echo "<b>Custom error:</b> [$errno] $errstr<br />";
echo " Error on line $errline in $errfile<br />";
echo "Ending Script";
die();
}
//set error handler
set_error_handler("customError");
$test=2;
//trigger error
if ($test>1)
{
trigger_error("A custom error has been triggered");
}
?>
结果
Custom error: [1024] A custom error has been triggered
Error on line 19 in C:webfoldertest.php
Ending Script
set_exception_handler()
作用
set_exception_handler() 函数设置用户自定义的异常处理函数。
该函数用于创建运行期间的用户自己的异常处理方法。
该函数返回旧的异常处理程序,如果失败则返回 NULL。
用法
set_exception_handler(exception_function)
exception_function 必需。规定未捕获的异常发生时调用的函数。
该函数必须在调用 set_exception_handler() 函数之前定义。这个异常处理函数需要需要一个参数,即抛出的 exception 对象。
测试案例
<?php
<?php
function myException($exception)
{
echo "<b>Exception:</b> " , $exception->getMessage();
}
set_exception_handler('myException');
throw new Exception('Uncaught Exception occurred');
?>
结果
Exception: Uncaught Exception occurred
参考
本作品采用《CC 协议》,转载必须注明作者和本文链接