Fix if condition php & and or in same if scope
P粉218775965
P粉218775965 2023-09-15 11:06:31
0
1
496

Tried adding&andorin the same if but the or condition doesn't work

else if ($usuario = 12 & $banca <=3 or =>7){ echo "ok"; }

is only valid when$banca <=3, but is invalid when$banca >=7What's wrong?

The range I'm trying to check is between <=3 和 >=7

P粉218775965
P粉218775965

reply all (1)
P粉257342166

There are some problems with your code. A simpler one and a more complex one: syntax and operator precedence.

First of all, operator=is used for assignment, not comparison. Therefore, the result of$usuario = 12is12. Next, you have a bitwise operator&that outputs0if$banca > 3, otherwise12. Finally, you take the result and check if it is>= 7. In other words: using them without knowing the operator won't do you any good. Please delve into the php language.

Although your syntax works in PHP, operator precedence can also mess up the results. Check out PHP'soperator precedence. This is one reason why linters and best practices manuals will instruct you to add parentheses to make your criteria clear, rather than relying onobscureoperator precedence.

Now, how do we solve this problem? Again, please take a closer look at PHP's operators and their syntax. There are some good courses out there, as well as theOfficial PHP Manual. But don't leave you hanging, your situation should look like this:

if ($usuario === 12 && ($banca <= 3 || $banca >= 7)) { ... }
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!