doudihuang7642 2016-12-29 16:31
浏览 105
已采纳

PHP检查| 变量赋值中的(OR)运算符

I want to check two variables a and b and assign both the variable to new variable "c" and want to get the OR result from both the variables. for example if a=1 and b=0, c must be 1, while if a=0 and b=1, c must be 1, if a=0 and b=0 then c=0, for this purpose i am using the following | operator, which returns the required result, but i am not sure if i am doing it correct or not

<?php 
$a = 0; 
$b = 1;     
$c = $a | $b;
echo("Value in $c = ".$c); 
?>

EDIT: i have gone through the PHP.NET website and find that:

 $a | $b    Or (inclusive or)   Bits that are set in either $a or $b are set.

Reference: http://php.net/manual/en/language.operators.bitwise.php

  • 写回答

1条回答 默认 最新

  • dongsi7759 2016-12-29 16:43
    关注

    Assuming you only have the states you have in your question, you can use a ternary to do this. It might help others understand what you're doing in the future

    $c = ($a || $b) ? 1 : 0;
    

    There's nothing wrong with the way you did it in your question, tho.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程