473,508 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Who thought this was a good idea?



if (0 == 'asdfasdf' ) echo 'WHAT THE FUCK??????????';

Jul 17 '05 #1
6 1387
mrbog wrote:
if (0 == 'asdfasdf' ) echo 'WHAT THE FUCK??????????';


I posted something about this the other day.

When comparing a string with a number the string is first evaluated as a
number, which in the case of the above is example is 0. So in effect
you are comparing 0 with 0.

A much safer test is

if(0 === "asdfasdf")

which will evaluate the condition as false. Note I'm using === here and
not ==

=== compares the types strictly whereas == does not.

And yes, I agree it's stupid. Very easy to catch people out.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #2
Hi,

Chris Hope wrote:
mrbog wrote:
if (0 == 'asdfasdf' ) echo 'WHAT THE FUCK??????????';
I posted something about this the other day.

When comparing a string with a number the string is first evaluated as a
number, which in the case of the above is example is 0. So in effect
you are comparing 0 with 0.


Sorry, this is not correct. The string and the number will be casted to
a boolean value. And a non empty string is definitly true. 0 will be
casted to false.

$ php -r 'if (0 == "asdf") echo "true\n"; else echo "false\n";'
true
if(0 === "asdfasdf")

which will evaluate the condition as false. Note I'm using === here and
not ==

=== compares the types strictly whereas == does not.


This is correct.

$ php -r 'if (0 === "asdf") echo "true\n"; else echo "false\n";'
false

Regards
Paul.
Jul 17 '05 #3
On 11/06/2005 11:35, Paul Wellner Bou wrote:
Chris Hope wrote:


[snip]
When comparing a string with a number the string is first evaluated as a
number, which in the case of the above is example is 0. So in effect
you are comparing 0 with 0.


Sorry, this is not correct.


Unless the documentation is wrong, it certainly is correct.

From Comparison Operators:

"If you compare an integer with a string, the string is converted to
a number."

and from Strings:

"If the string starts with valid numeric data, this will be the value
used. Otherwise, the value will be 0 (zero)."

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 17 '05 #4
Paul Wellner Bou wrote:
Chris Hope wrote:
mrbog wrote:
if (0 == 'asdfasdf' ) echo 'WHAT THE FUCK??????????';


I posted something about this the other day.

When comparing a string with a number the string is first evaluated
as a number, which in the case of the above is example is 0. So in
effect you are comparing 0 with 0.


Sorry, this is not correct. The string and the number will be casted
to a boolean value. And a non empty string is definitly true. 0 will
be casted to false.


It is correct. It's in the manual. Check the first example in the code
block at the following manual page:
http://www.php.net/manual/en/languag...comparison.php

If you were right, then the above code snippet would not output the
text. Try running the code; it *does* output the text.

[rest of comments snipped]

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #5
Paul Wellner Bou said the following on 11/06/2005 11:35:
Hi,

Chris Hope wrote:
mrbog wrote:
if (0 == 'asdfasdf' ) echo 'WHAT THE FUCK??????????';

I posted something about this the other day.

When comparing a string with a number the string is first evaluated as a
number, which in the case of the above is example is 0. So in effect
you are comparing 0 with 0.

Sorry, this is not correct. The string and the number will be casted to
a boolean value.


Wha?? Since when does that ever happen? If you did something like:

if (4 == 7) ...

4 and 7 are most definitely *not* converted to Booleans. The same
applies for the example above.
--
Oli
Jul 17 '05 #6
Chris Hope wrote:
Paul Wellner Bou wrote:
Chris Hope wrote:
When comparing a string with a number the string is first evaluated
as a number, which in the case of the above is example is 0. So in
effect you are comparing 0 with 0.


Sorry, this is not correct. The string and the number will be casted
to a boolean value. And a non empty string is definitly true. 0 will
be casted to false.


It is correct. It's in the manual. Check the first example in the code
block at the following manual page:
http://www.php.net/manual/en/languag...comparison.php

If you were right, then the above code snippet would not output the
text. Try running the code; it *does* output the text.


Sorry... I was confused. Shame on me.
The code sample I gave contradicts my words.

Ignore my post... ;)

Regards
Paul.
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

100
8948
by: Peter | last post by:
Company thought DB2 will be better than Oracle. The bottom line is when you do select, the system crash. I think it may take 4-5 years for DB2 to reach Oracle standard. Peter
12
15492
by: Generic Usenet Account | last post by:
I am going through some legacy code that has an "isNull()" method defined on certain classes. I can see that this can be a good way to eliminate certain types of crashes, by making this the first...
116
7415
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data...
14
4598
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further...
43
2604
by: Sensei | last post by:
Hi! I'm thinking about a good programming style, pros and cons of some topics. Of course, this has nothing to do with indentation... Students are now java-dependent (too bad) and I need some...
150
6322
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and...
54
3583
by: sam.s.kong | last post by:
Hi! I've been programming ASP for 5 years and am now learning PHP. In ASP, you can use GetRows function which returns 2 by 2 array of Recordset. Actually, it's a recommended way in ASP when you...
4
2349
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
9
2149
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
5
1689
by: mike3 | last post by:
Hi. Is this a good idea?: <begin code> /* Addition operator: += */ const BigFix &BigFix::operator+=(const BigFix &rhs) { ErrorType err; int lhs_sign = sign, rhs_sign = rhs.sign;
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.