• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

PHP中类似$a && $b = $c 语法的用法和实例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

$a && $b = $c 表示:如果$a为真,则执行$b = $c,否则不执行。

可以用if语句替代:

if ($a) {
    $b = $c;
}

其他php7写法:

$a = 1;$b = 2;$c = 3;
$a? :$b=$c;
echo "a:$a";
echo "b:$b";
echo "c:$c";
//输出是
a:1b:2c:3
$a = 0;$b = 2;$c = 3;
$a? :$b=$c;
echo "a:$a";
echo "b:$b";
echo "c:$c";
//输出是
a:0b:3c:3

如果$a 为真则执行$b 否则不执行

return $a && $b;

如果$a 为真则执行$a=$b 否则不执行

$a && $a=$b;

 注意:这是种写法,但在项目里一般是不允许这样写,一是这种语法那天不支持了,二是给别人造成阅读上的障碍,代码风格难以统一

 

实例1:

$a = 1;$b = 2;$c = 3;
$a && $b = $c;
echo "a:$a";
echo "b:$b";
echo "c:$c";
//输出是
a:1 b:3 c:3

实例2:

$a = 0;$b = 2;$c = 3;
$a && $b = $c;
echo "a:$a";
echo "b:$b";
echo "c:$c";
//输出是
a:0 b:2 c:3

实例3:

$a = 1;$b = 0;$c = 3;
$a && $b = $c;
echo "a:$a";
echo "b:$b";
echo "c:$c";
//输出是
a:1 b:3 c:3

实例4:

$a = 1;$b = 2;$c = 3;
$a ||$b = $c;
echo "a:$a";
echo "b:$b";
echo "c:$c";
//输出是
a:1 b:2 c:3

实例5:

$a = 0;$b = 2;$c = 3;
$a || $b = $c;
echo "a:$a";
echo "b:$b";
echo "c:$c";
//输出是
a:0 b:3 c:3

实例6:

$a = 1;$b = 2;$c = 3;$d = 4;$e = ['10'];
$a && $b && $c != $d && $e[] = $c;
print_r(['$a'=>$a,'$b'=>$b,'$c'=>$c,'$d'=>$d,'$e'=>$e]);
//输出是
Array
(
    [$a] => 1
    [$b] => 2
    [$c] => 3
    [$d] => 4
    [$e] => Array
        (
            [0] => 10
            [1] => 3
        )

)

实例7:

$a = 1;$b = 0;$c = 3;$d = 4;$e = ['10'];
$a && $b && $c != $d && $e[] = $c;
print_r(['$a'=>$a,'$b'=>$b,'$c'=>$c,'$d'=>$d,'$e'=>$e]);
输出是
Array
(
    [$a] => 1
    [$b] => 0
    [$c] => 3
    [$d] => 4
    [$e] => Array
        (
            [0] => 10
        )

)

实例8:

$a = 0;$b = 2;$c = 3;$d = 4;$e = ['10'];
$a && $b && $c != $d && $e[] = $c;
print_r(['$a'=>$a,'$b'=>$b,'$c'=>$c,'$d'=>$d,'$e'=>$e]);
//输出是
Array
(
    [$a] => 0
    [$b] => 2
    [$c] => 3
    [$d] => 4
    [$e] => Array
        (
            [0] => 10
        )

)

实例9:

$a = 1;$b = 2;$c = 3;$d = 4;$e = ['10'];
$a && $b && $c != $d && $e[] = $c;
print_r(['$a'=>$a,'$b'=>$b,'$c'=>$c,'$d'=>$d,'$e'=>$e]);
//输出是
Array
(
    [$a] => 1
    [$b] => 2
    [$c] => 3
    [$d] => 4
    [$e] => Array
        (
            [0] => 10
            [1] => 3
        )

)

实例10:

$a = 1;$b = 2;$c = 3;$d = 3;$e = ['10'];
$a && $b && $c != $d && $e[] = $c;
print_r(['$a'=>$a,'$b'=>$b,'$c'=>$c,'$d'=>$d,'$e'=>$e]);
//输出是
Array
(
    [$a] => 1
    [$b] => 2
    [$c] => 3
    [$d] => 3
    [$e] => Array
        (
            [0] => 10
        )

)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++智能指针剖析(上)std::auto_ptr与boost::scoped_ptr发布时间:2022-07-13
下一篇:
C#操作word(转)4发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap