分类目录归档:php

php restore_exception_handler

restore_exception_handler ()函数恢复PHP自身的异常处理。会清除set_exception_handler中用户自定义的函数处理方式

例子:

function handleException(Exception $e)
{ 
    do{
        echo "<b>错误信息</b>:" . $e->getMessage() . "</br>";
        echo "<b>错误码</b>:" . $e->getCode() . "</br>";
        echo "<b>错误文件</b>:" . $e->getFile() . "</br>";
        echo "<b>错误行数</b>:" . $e->getLine() . "</br>";
        echo "<b>错误行数</b>:" . $e->getPrevious() . "</br>";
        echo "<b>异常追踪信息</b>:</br>";
        echo "</pre>" . print_r($e->getTrace(), true) . "</pre></br>";
        echo "***********</br>";
    } while($e = $e->getPrevious());
}

set_exception_handler("handleException");

function test($name) {
    throw new Exception("Test exception", 1000);
}

restore_exception_handler();
test("codyi");

结果:

Fatal error: Uncaught exception 'Exception' with message 'Test exception' in /Users/liguosong/work/php/Testing/test.php on line 21 Exception: Test exception in /Users/liguosong/work/php/Testing/test.php on line 21 Call Stack: 0.0002 251616 1. {main}() /Users/liguosong/work/php/Testing/test.php:0 0.0003 252240 2. test(???) /Users/liguosong/work/php/Testing/test.php:25

 

 

php Exception 详解

Exception 类是php所有异常的基类。这个类包含如下方法:

__construct — 异常构造函数
getMessage — 获取异常消息内容
getPrevious — 返回异常链中的前一个异常
getCode — 获取异常代码
getFile — 获取发生异常的程序文件名称
getLine — 获取发生异常的代码在文件中的行号
getTrace — 获取异常追踪信息
getTraceAsString — 获取字符串类型的异常追踪信息

以上方法的简介直接从PHP官网抄来的。

下面写个例子:

function handleException(Exception $e)
{ 
    do{
        echo "<b>错误信息</b>:" . $e->getMessage() . "</br>";
        echo "<b>错误码</b>:" . $e->getCode() . "</br>";
        echo "<b>错误文件</b>:" . $e->getFile() . "</br>";
        echo "<b>错误行数</b>:" . $e->getLine() . "</br>";
        echo "<b>错误行数</b>:" . $e->getPrevious() . "</br>";
        echo "<b>异常追踪信息</b>:</br>";
        echo "</pre>" . print_r($e->getTrace(), true) . "</pre></br>";
        echo "***********</br>";
    } while($e = $e->getPrevious());
}

set_exception_handler("handleException");

class MyException extends Exception {}

function test($name) {
    throw new Exception("Test exception", 1000);
}

try{
    test("codyi");
} catch (Exception $ex) {
    throw new MyException("My exception", 1001, $ex);
}

输出结果:

错误信息:My exception
错误码:1001
错误文件:/Users/liguosong/work/php/Testing/test.php
错误行数:27
错误行数:exception 'Exception' with message 'Test exception' in /Users/liguosong/work/php/Testing/test.php:21 Stack trace: #0 /Users/liguosong/work/php/Testing/test.php(25): test('codyi') #1 {main}
异常追踪信息:
Array ( ) 
***********
错误信息:Test exception
错误码:1000
错误文件:/Users/liguosong/work/php/Testing/test.php
错误行数:21
错误行数:
异常追踪信息:
Array ( [0] => Array ( [file] => /Users/liguosong/work/php/Testing/test.php [line] => 25 [function] => test [args] => Array ( [0] => codyi ) ) ) 
***********

 

php set_error_handler

set_error_handler() 设置用户自定义的错误处理函数。在程序的运行期间,通过这个函数可以将错误信息按照我们自己的函数处理。

例子:

function handleError($errno, $errstr, $errfile, $errline)
{ 
    echo "<b>Custom error:</b> [$errno] $errstr<br />";
    echo " Error on line $errline in $errfile<br />";
}

set_error_handler("handleError");

$test = 2;

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 /Users/liguosong/work/php/Testing/test.php

 

php restore_error_handler

restore_error_handler()函数恢复PHP自身的错误处理程序。从下面的例子中,可以看出,当执行restore_error_handler()函数之后,我们自己定义的错误处理函数已经不在起作用了。

例子:

function handleError($errno, $errstr, $errfile, $errline)
{ 
    echo "<b>Custom error:</b> [$errno] $errstr<br />";
    echo " Error on line $errline in $errfile<br />";
}

set_error_handler("customError");

$test = 2;

if ($test>1)
{
    trigger_error("A custom error has been triggered");
}

//恢复PHP本身的错误处理。正常显示trigger信息
restore_error_handler();

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 /Users/liguosong/work/php/Testing/test.php
Notice: A custom error has been triggered in /Users/liguosong/work/php/Testing/test.php on line 22 Call Stack: 0.0096 253080 1. {main}() /Users/liguosong/work/php/Testing/test.php:0 0.0100 253952 2. trigger_error(???) /Users/liguosong/work/php/Testing/test.php:22

 

php strstr

strstr()函数返回字符串中的一部分。

strstr(string,search,
string:必须,规定被搜索的字符串
search:必须,搜索字符串
before_needle:可选,默认值是false。如果是true,从被搜索字符串往前开始截取。如果是false,从搜索字符串开始,一直到结尾

例子:

echo strstr("Hello world.", "w");

结果:

world.

例子:

echo strstr("Hello world.", "w", true);

结果:

Hello

 

)

 

升级php版本到php5.5,掉坑了。。。。

昨天刚刚升级了系统跟php的版本。把php升级到了5.5。升完级后,感觉挺爽。

结果一会服务器就出现了502的错误,有些懵了。。。后来经过排查,发现原来ngnix的一个配置不管用了。就是fastcgi_pass。原来是转到一个9000的端口上。升级后,可能php的配置文件变了。这个不管用了。 继续阅读

php5.4 新特性 trait

php5.4中的trails,是新引入的特性。目的是为了有得场合理的让代码实现重用。

注意:trails不是类,不能实例化。如果trails的方法名和类中的方法名冲突,类中的方法名会覆盖trails的方法名。说明类中的方法名,优先级更高。 继续阅读

golang,java,python,nodejs,php计算斐波那契数列速度对比

同样的程序,这五种语言速度对比。java速度最快,其次golang,nodejs,python,php。在这里php,python已经与前面的语言不在同一个级别了。据说python可以在速度上优化一下,但我现在还处于对python了解阶段。并没有过多的深入研究。 继续阅读