Jump to content

Missing Values


iwato

Recommended Posts

PROBLEM: Please consider the following two blocks of code. Each contains several echo statements; each returns a value indicating that the variable types of $return_same and $return_identical are boolean. When I go to display the value of $return_identical itself; however, nothing appears.

$return_same = ($bar == $foo);echo is_bool($return_same) . "<br />";echo var_dump($return_same) . "<br />";echo "$return_same <br /><br />";$return_identical = ($bar === $foo);echo is_bool($return_identical) . "<br />";echo var_dump($return_identical) . "<br />";echo "$return_identical <br /><br />";echo is_null($return_identical);

The output for the above two blocks of code is as follows:1bool(true) 1 1bool(false) QUESTION: With the exception of what is echoed for the var_dump() function, why is no value echoed for the $return_identical variable? Also, why is nothing displayed for the is_null() function?Roddy

Link to comment
Share on other sites

a boolean false evaluates to an emptry string. This explains it all.

Link to comment
Share on other sites

A boolean false evaluates to an emptry string. This explains it all.
Well, it explains a lot anyway.What it does not explain is the following. Can you?
<?php	$bar = "";	$foobar = "0";	if ($bar === $foobar) {		echo "The empty set and zero entered as a string are identical.<br />";	} else {		echo "The empty set and zero entered as a string are not identical.<br />";		if ($bar == $foobar) {			echo "The empty set and zero entered as a string are the same, but not identical.";		} else {			echo "This evaluation of the empty set and zero entered as a string is inconclusive.";		}	}?>

OUTPUTThe empty set and zero entered as a string are not identical.This evaluation of the empty set and zero entered as a string is inconclusive.Roddy

Link to comment
Share on other sites

can't see what problem is'$bar === $foobar' = same type (string) and same string value = FALSEecho "The empty set and zero entered as a string are not identical.<br />";'$bar == $foobar' = same string value = FALSE againecho "This evaluation of the empty set and zero entered as a string is inconclusive.";

Link to comment
Share on other sites

The integer 0 is equivalent to the boolean false and the string "". The string "0" is not the same as the string "". Any string other than "" is equivalent to a boolean true.If you first cast the string "0" to an integer, and then to a boolean, you'll get a boolean false.See comparrison tables.

Link to comment
Share on other sites

To help clarify, since both of the arguments are strings it does not convert them, it compares the string values. If one of them was the number 0 or a boolean false then it would convert and the results would be different.

Link to comment
Share on other sites

The integer 0 is equivalent to the boolean false and the string "". The string "0" is not the same as the string "". Any string other than "" is equivalent to a boolean true.If you first cast the string "0" to an integer, and then to a boolean, you'll get a boolean false.See comparrison tables.
The PHP Type Comparison Table will surely come in handy in the future.Thanks for making me aware of it.Roddy
Link to comment
Share on other sites

To help clarify, since both of the arguments are strings it does not convert them, it compares the string values. If one of them was the number 0 or a boolean false then it would convert and the results would be different.
This was a very helpful clarification. No conversion.Roddy
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...