trycatchfinally 清疚 2023-06-23 15:24 16阅读 0赞 <?php function test() { $str = "hello"; try { return $str; } finally { $str = "world"; } } function test1() { $arr = new stdClass(); $arr->name = 'silk'; try { return $arr; } finally { $arr->name = "xiaoyang"; } } function test2() { $arr = [ 'name' => 'silk', 'age' => 18 ]; try { return $arr; } finally { $arr['name'] = "xiaoyang"; } } function test3() { $i = 0; try { return ++$i; } finally { $i = $i + 2; } } function test4() { $i = 0; try { // return ++$i; throw new Exception("new Exception"); } catch (Exception $e) { return --$i; } finally { return --$i; } } var_dump(test()); var_dump(test1()); var_dump(test2()); var_dump(test3()); var_dump(test4());
还没有评论,来说两句吧...