异常处理
# 异常处理
function checkfun($num)
{
if($num > 10)
{
# 返回一个异常错误
throw new Exception('Value must be 1 or berow');
}
return "正确";
}
# 判断是否 会触发我们 实现定义好的异常提示
try{
# 如果传入的变量大于10则返回一异常
$res = checkfun(11);
echo $res;
}catch(\Exception $e){
# 否则返回一个异常,在echo出去
$error = $e->getMessage();
echo $error;
}
# php 过滤敏感词
# 过滤词函数
function replace($text)
{
$badwords = [
'sb' => '好',
'moronic' => 'beautiful',
'ugly' => 'handsome',
'山炮' => '时髦'
];
# php中的 类似替换函数,处理敏感词
return strtr($text,$badwords);
}
# 测试方法
$res = replace('sb');
echo $res;
echo '<br>';
# 中文,如果在liunx上运行出现中文乱码,可以使用urlencode进行转码
$chinese = replace('山炮');
echo $chinese;