Refactoring legacy code I have found various occurrences of the following construct:
((bool) ~~$field->req ? ' required' : '')
According to the manual the Tilde (~) in PHP is the bitwise not, which simple shifts all bits in a variable to their opposite.
If I shift all bits in a variable to their opposite and then shift it back, the variable should be exactly the same as it was before right? So why would somebody do this? Am I missing something?
It should be
!!(it converts the value to a boolean) but it is not needed at all. I guess the original coder mistaken~for!then they added(bool)in front of it to achieve the desired result (because, as you noticed in the question,~~is a no-op).The ternary operator (
?:) forces the evaluation of its first argument as boolean.The boolean value of
$field->reqis the same as of!! $field->reqand(bool) ~~$field->req(and(bool)$field->reqbtw).I would remove the
(bool) ~~part completely to get smaller and cleaner code.Edit by questioner: The only effect of ~~ in PHP is to cut of decimals from a float value.
See the following results: