Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP if statement question (& operator)

I'm using a function I found online. What does the & mean in this conditional?

if ($strength & 8) { $consonants .= '@#$%'; }

$strength is supposed to be a number 0-8. The function is intending to use all $consonants concatenations where $strength < 8. (might explain why the function is not working).

like image 996
Kyle Parisi Avatar asked Dec 28 '22 03:12

Kyle Parisi


2 Answers

A single & is the bitwise operator and the double && is the logical. (i.e. Bits that are set in both $strength and 8 are set in your example.) It's a lot more complicated than just saying that and it requires an understanding of how binary works.

EDIT: Check out this article for more information on Bitwise operators.

like image 126
Michael Irigoyen Avatar answered Jan 05 '23 16:01

Michael Irigoyen


& is a bitwise operator - it's checking to see if the bits that total 8 are set. In this case, 1000

like image 23
Demian Brecht Avatar answered Jan 05 '23 18:01

Demian Brecht