开发者

What is the meaning of "|" in this PHP code? [duplicate]

开发者 https://www.devze.com 2023-04-01 16:35 出处:网络
This question already has an开发者_运维问答swers here: Closed 11 years ago. Possible Duplicate: Reference - What does this symbol mean in PHP?
This question already has an开发者_运维问答swers here: Closed 11 years ago.

Possible Duplicate:

Reference - What does this symbol mean in PHP?

Below is the PHP code:

<?php 
    $a = $b['key'] | 0;
?>

Is it an operator?


The | is the bitwise OR operator.

Doing a bitwise OR with zero (| 0) doesn't make any sense though, as it will not flip any bits. Maybe the guy who wrote this was just a really bad programmer and tried to cast a string to an integer that way. He should have used a (string) cast instead!


Its bitwise OR, as defined here.

For example:

 Bitwise Inclusive OR
( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101)
( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101)
( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101)
( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101)
(13 = 1101) = ( 8 = 1000) | ( 5 = 0101)


| is a bitwise OR function.

|| is a regular OR


It is the bitwise OR operator.

See here for more details:

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


It is performing a bitwise OR operation.

It is possible the original author of the code meant to put another | in to make it a logical OR (||), because a bitwise OR with 0 will have no effect whatsoever on the output. Although even this makes no sense, he could have simply cast to an integer to get the same result.


| is a bitwise operator (http://php.net/manual/en/language.operators.bitwise.php)

| means "Bits that are set in either $b['key'] or 0 are set."

Because the second part is a zero though, it will return false only if $b['key'] is zero too.

0

精彩评论

暂无评论...
验证码 换一张
取 消