PHP 抛出异常 vs true or false
本文介绍了PHP 抛出异常 vs true or false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
使用布尔进行逻辑处理。
$edit //一段数据库操作
if ($edit) {
return redirect('back/label/index')->with('message', '修改成功');
} else {
return redirect('back/label/index')->with('message', '修改失败');
}
PHP 异常处理 ,代码来源
<?php
//创建可抛出一个异常的函数
function checkNum($number)
{
if($number>1)
{
throw new Exception("Value must be 1 or below");
}
return true;
}
//在 "try" 代码块中触发异常
try
{
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
}
//捕获异常
catch(**Exception $e**)
{
echo 'Message: ' .$e->getMessage();
}
?>
希望大神能讲下两种的区别,以及各自的好处,谢谢。
解决方案
ifelse:更加直观 适合逻辑较为简单的处理
异常:更加灵活 适合逻辑复杂 层次较多的处理
我自己平时一般使用异常来做的
这篇关于PHP 抛出异常 vs true or false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文