4、运算符

 

一 [ 基础运算 ]

加 减 乘 除 取模 自加 自减
1、使用了过滤函数后 就不能使用运算符
{$user.score+10} //正确的
{$user['score']+10} //正确的
{$user['score']*$user['level']} //正确的
{$user['score']|myFun*10} //错误的
{$user['score']+myFun($user['level'])} //正确的

 

二 [ 三元运算符 ]

1)基础三元

$arr = [
'status' => 1,
'success' => 'ok',
'error' => 'not ok'
];
return View::fetch('',['info' => $arr]);
{$info['status'] ? '正确' : '错误'}
{$info['status'] ? $info['success'] : $info['error']} 

2)三元 + isset 判断

isset() — 检测变量是否已设置并且非 NULL

{$name ?? '默认值'} //$name没有设置显示默认值

<?php echo isset($name) ? htmlentities($name) : '默认值'; ?>

3)三元  + 非空验证

return View::fetch('',['name' => '']);

{$name ?: 'NO'} //$name为真输出$name 否则输出NO

<?php echo !empty($name) ? htmlentities($name) : 'NO'; ?>//解析后的代码

4)三元 + 比较运算

$arr = ['a' => '10', 'b' => '10'];
return View::fetch('',['info' => $arr]);
{$info[
'a'] == $info['b'] ? 'yes' : 'no'}

//解析后的代码
<?php echo $info['a'] == $info['b'] ? 'yes' : 'no'; ?> 条件可以是: == === != !== >= <=

5)if + empty 判断

return View::fetch('',['name' => '张飞']);

{$name ?= '默认值'}  //$name为真显示默认值

<?php if(!empty($name)) echo '默认值'; ?>

 

posted @ 2021-01-06 09:21  棉花糖88  阅读(93)  评论(0)    收藏  举报