|之间的区别 和|| 在PHP中[重复]

This question already has an answer here:

I have simple code

$value = 5;
$string = 'Abc';

var_dump(($value > 0) || (strlen($string) == 2));
var_dump(($value > 0) | (strlen($string) == 2));

Only what is changed is type of returned value (first is boolean, second int). There is another difference between | and ||? Can I change one to another?

Live test: http://sandbox.onlinephpfunctions.com/code/548ab723cbd156be70a596978427fbd73ce4639f

</div>

var_dump(($value > 0) || (strlen($string) == 2));

|| is a logical logical operatpor, see http://php.net/manual/de/language.operators.logical.php

var_dump(($value > 0) | (strlen($string) == 2));

| is a bitwise operator, see http://php.net/manual/de/language.operators.bitwise.php

Sure, you can change | to ||, but you won't get the same result ;) A little explanation for your code, but you should really read the doc for bit- and logical operators:

You already answered, that both don't do the same:

var_dump(($value < 0) || (strlen($string) == 2)); -> returns a boolean true

var_dump(($value < 0) | (strlen($string) == 2)); -> returns an integer 1

If you do:

var_dump(true === 1);

You will get false, because integer 1 isn't a boolean true, even if:

var_dump(true == 1);

or

var_dump(true === (bool)1);

will return true (== doesn't check for type, see the docs, and (bool) casts the integer 1 to be a boolean true (see http://php.net/manual/de/language.types.boolean.php#language.types.boolean.casting to know what is false and what is true).