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
https://forums.phpfreaks.com/topic/79916-value-of-0/
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
https://forums.phpfreaks.com/topic/79916-value-of-0/#findComment-404762
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
https://forums.phpfreaks.com/topic/79916-value-of-0/#findComment-404879
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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