Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To use | or || in an if statement

Status
Not open for further replies.

timtrust123

Programmer
Dec 19, 2003
19
GB
Hi

I'm a little confused as to the useage of either | or ||

should I use....

if ($ftp_file_size < $lower_size | $ftp_file_size > $upper_size )

or should i use

if ($ftp_file_size < $lower_size || $ftp_file_size > $upper_size )

Whats the difference beteween the two?

HELP appreciated
 
'|' is a bitwise OR operator so works differently with numbers than it does to strings.

'||' is a logical OR operator which evaluates from the left to right of a string or number matching as it goes.

SO basically you should use '|' to compare integers and '||' to compare strings, although '||' will compare integers as well as strings, whereas '|' won't compare strings!!

Rob Waite
 
The basic rule of thumb is to use || unless there's a reason you need to perform bitwise operations on numbers.

When used in the context of your example (i.e. simply evaluating for truth), | happens to have a similar effect to || when used on numbers but it's not really what it's designed for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top