Jump to content

Value of 0


paradigmapc

Recommended Posts

I'm sure there is a perfectly logical explanation behind this but why does

print (0 == "STRING") ? "0 is equal to STRING" : "0 is not equal to STRING";

print

0 is equal to STRING

 

no need to say to the identity operator "===". I'm just curious to the reason of why "STRING" would be converted to zero during the comparison.

 

Thanks

Link to comment
Share on other sites

This code still prints "0 = STRING":

<?php
if(0 == "STRING"){
echo "0 = STRING";
}
else{
echo "0 != STRING";
}
?>

as does:

print (0 == "STRING") ? "0 is equal to STRING" : "0 is not equal to STRING";

 

I think you may have misunderstood by question:

Within the if statement 0 and "STRING" are being compared but because the identity operator (===) was not used string is being converted to a int in order to be compared. Why does a string convert to 0 when being compared to an int?

 

My guess is that what ever is converting the string to an int is returning zero because it cant be converted, but I am not sure, and then again my whole thought process of the event could be wrong.

 

Sorry for the confusion.

Link to comment
Share on other sites

This looks like a bug to me. "STRING" should actually equate to 1 (true).

 

<?php

  echo ('STRING') ? 'true' : 'false'; // returns as expected true.
  echo ('STRING' == TRUE) ? 'true' : 'false'; // returns as expected true.
  echo ('STRING' == FALSE) ? 'true' : 'false'; // returns as expected false.
  echo ('STRING' == 0) ? 'true' : 'false'; // returns as NOT expected true.

?>

 

Link to comment
Share on other sites

This looks like a bug to me. "STRING" should actually equate to 1 (true).

 

Nope.

 

(int) 'STRING' = 0;

(bool) 'STRING' = TRUE;

 

Yeah, I realise now that a string is converted to an int (with a value of 0) when compared with an int.

Link to comment
Share on other sites

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.