Forum Moderators: coopster & phranque

Message Too Old, No Replies

Difference between these two "If" statements

         

lindajames

8:21 am on May 21, 2003 (gmt 0)

10+ Year Member



Hi, can anyone please tell me what the difference between the two following "If" statements are?

Cheers
Linda

if ($nnb eq '')
{
print "Location: $invalid\n\n";
exit;
}

============================

if ($nnb == "")
{
print "Location: $invalid\n\n";
exit;
}

=========================

Robber

8:39 am on May 21, 2003 (gmt 0)

10+ Year Member



Hi,

You use the eq if you are comparing strings and == for numbers. Also, you shouldn't need quotes when using == as it is comparing numbers.

Lastly the difference between single quotes and double quotes is that double quotes support interpolation (if you use a variable in double quotes the variable value will come through - with single quotes it wont.

Cheers

tzwoenn

12:28 pm on May 22, 2003 (gmt 0)



Depending on the programming language, "a == b" will compare so called primitive variable typs like INT, BOOL, DOUBLE as u suppose: the containing numbers will be compared.
If a, b arent primitives (like STRING or OBJECT e.g.), there reference will be compared. This means "a == b" tests, if a and b point on the same datastructure. If this is true and u assign "a" a new value, "b" is changed too.
EQUALS or sometimes also EQ are function of these nonprimitives, that will compare the value of there reference. In this case, if u change "a", "b" still has the old value.

ShawnR

1:19 pm on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi tzwoenn, and welcome to WebmasterWorld!

In the most welcoming way possible, I'd like to challenge some of what you posted ;) Your post obviously shows that you are knowledgable and have a love for a good debate to uncover the truth, so I am guessing that you won't mind me challenging your statement.

>>"...Depending on the programming language..."
From what I can see, Linda's question is a perl question. In perl, a string is a 'primitive', and yet ($string1 == $string2) won't compare the string primitives, it will compare the value of the string if it can be converted to a number (also might result in a 'compiler' warning depending on the how strict perl is being invoked). Example:

if ($str == 0 && $str ne "0") {
warn "That doesn't look like a number";
}

In perl, when you use == on a more complex data structure (say an array), then you'd be doing it in a scalor context (else you'd get a compiler error), in which case you'd be comparing the number of elements in the array, not whether the array references are the same.

So that is how it is in perl. To be honest, if I think through a range of other languages (C, C++, java, javascript, php, fortran, ada, Visual Basic, pascal, ...) I can't find one language where your definition is true, although I'd agree it would be a neat definition in a generic sense.

Have I missed your point?

Shawn