Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Boolean operator question
Old 06-28-2006, 11:47 PM Boolean operator question
Webmaster Talker

Posts: 626
Trades: 0
Is it possible to compare to multiple values in a single boolean statement?

For example, I have a CSV file which contains about 60 lines of information. I have read the file and saved the information into a multi dimensional array which is accessible as $arr_info[$row][$cell]. Now I want to access just rows 0,2,46-56.

I was trying to do it in a loop (this is contained in a while loop with a counter $cnt):
Code:
  if($cnt == (0 | 2 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54)) { 
      echo $arr_info[$cnt][1]."<br>";
}
I know that the information is in the array because I can echo the whole array without a problem.. However, I'm trying to have just one boolean operator statement (if) that covers all values considering the code that is being executed is the same.

My code isn't working... Can anyone help out!
jim.thornton is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-29-2006, 03:14 AM Re: Boolean operator question
ChancesAre's Avatar
Skilled Talker

Posts: 84
Trades: 0
You can try in_array() function.
ChancesAre is offline
Reply With Quote
View Public Profile
 
Old 06-29-2006, 03:47 AM Re: Boolean operator question
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Quote:
Originally Posted by zincoxide
Code:
  if($cnt == (0 | 2 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54)) { 
      echo $arr_info[$cnt][1]."<br>";
}
What you've got there is a bitwise or, not a boolean or. A bitwise or takes the binary representation of one operand and then ORs each bit with the binary representation of the other. Thus 12 | 5 = 13 since 12 is 1100 in binary and 5 is 0101. ORing each bit gives 1101 = 13.

To change the code to do a boolean OR, you would need to re-write it as follows:
PHP Code:
  if($cnt == || $cnt == || $cnt ==  46 || $cnt == 47 || $cnt ==  48 || $cnt ==  49 || $cnt ==  50 || $cnt ==  51 || $cnt ==  52 || $cnt ==  53 || $cnt ==  54)) { 
      echo 
$arr_info[$cnt][1]."<br>";

Note that a boolean OR is || not |

Of course this is very long winded and it would be much better to do something like
PHP Code:
if( ($cnt >= 46 && $cnt <= 54) || $cnt == || $cnt == 2) {
 echo 
$arr_info[$cnt][1]."<br>";

__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 06-29-2006, 10:04 AM Re: Boolean operator question
Webmaster Talker

Posts: 626
Trades: 0
Thanks.
jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 06-29-2006, 12:08 PM Re: Boolean operator question
AliKat's Avatar
Extreme Talker

Latest Blog Post:
Save the Children
Posts: 176
Location: MS
Trades: 0
or use a switch statement

PHP Code:
switch ($cnt)
{
 case 
'0','2','46','47','48','49','50','51',' 52','53','54':
      echo 
$arr_info[$cnt][1]."<br>";
      break;
 
defulat:
     break;

AliKat is offline
Reply With Quote
View Public Profile Visit AliKat's homepage!
 
Reply     « Reply to Boolean operator question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29763 seconds with 12 queries