PHP check | (OR) operator in variable assignment

866 Views Asked by At

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

There are 1 best solutions below

4
On BEST ANSWER

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.