Jump to content

Recommended Posts

 

<?php

if('00' == '000')

echo 'weired';

?>

 

 

Please consider the code above. Strangely it echoes the word "weired".

 

Also if('00' == '000000000000000') returns true and so forth.

 

What is really going on?

 

Thanks in advance

 

By the way the php version is 5.3.9 on wampserver 2.2

 

Link to comment
https://forums.phpfreaks.com/topic/257521-if00-0000-returns-true/
Share on other sites

if nothing == nothing then its true!

 

if '000' === '000' if EXACTLY the same then true

 

its like when you have a 0 return for some function which can be seen as false and not 0

 

so you use 3 equals signs!

 

(i think i didnt explain very well but I hope you get the idea)

For some reason it was decided that if you're loosely comparing two numeric strings, PHP will compare them as numbers.

 

Comparison Operators

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

 

As random as spiderwell's comment is, he actually said the right answer: use a strict comparison instead.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.