Jump to content

[SOLVED] confused about OR syntax


wds

Recommended Posts

when i test this code:

<?

$i = 0;

while($i < 20){

if(($i / 5) == (1 || 2 || 3)){

print($i . "   " . ($i / 5) . "<br>");

}

$i++;

}

?>

 

It outputs:

1   0.2

2   0.4

3   0.6

4   0.8

5   1

6   1.2

7   1.4

8   1.6

9   1.8

10   2

11   2.2

12   2.4

13   2.6

14   2.8

15   3

16   3.2

17   3.4

18   3.6

19   3.8

 

 

Yet this code:

<?

$i = 0;

while($i < 20){

if(($i / 5) == 1 || ($i / 5) == 2 || ($i / 5) == 3){

print($i . "   " . ($i / 5) . "<br>");

}

$i++;

}

?>

 

Gives me the correct output:

5   1

10   2

15   3

 

 

i don't understand why i can't use the previous code, not that it really matters, it just seems tedious to have to write all that for every time. any ideas why

($i / 5) == (1 || 2 || 3)

does not work, yet

($i / 5) == 1 || ($i / 5) == 2 || ($i / 5) == 3

does work?

Link to comment
https://forums.phpfreaks.com/topic/63976-solved-confused-about-or-syntax/
Share on other sites

That is simply the syntax. The == operator can only support one value on either side). By the time it gets to the || operator, the comparisons have already been done and it is only separating the true or false values. so when you do if(($i / 5) == (1 || 2 || 3)), you are comparing $i/5 to (true or true or true).

if(($i / 5) == (1 || 2 || 3)){
[/cdoe]

i guess doing something like that will be the same as


if(($i / 5) == bool){
(1 || 2 || 3) == boolean value witch is true or false
so doing this if(($i / 5) == (1 || 2 || 3)){ returns true or false in this case i guess your getting true because 0 is false and non zero is true so you always get true

why?
($i / 5) is not zero 

so its like saying true = true 


hope that helps

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.