Can't figure out the error.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Here is some code I can't find the error in. I'm trying to fill a <select>
with option coming from a database, and would like to select a previously
selected record. For this, I set the value $City with the code selected
before. For testing purpose, I set the $City value to 0.
Now, I've 3 records in my table. ZIP fields are: 1950,1,a
When I run the code below, $value becomes "a".
That means that a = = 0; ??? but why ????

function showmessage($text){
    echo("<script>alert('".$text."');</script>"); //debug function

$City = 0; //for testing purpose
    $query_RSCity = "SELECT DISTINCT ZIP, Cityname From person where
person.IDType > 1;";
    if(($RSCity = mysql_query($query_RSCity, $shopauto))){
     while($QRow = mysql_fetch_object($RSCity)){
      echo("<OPTION VALUE=\"".$QRow->ZIP."\"");
      if ($QRow->ZIP == $City){
       echo(" selected ");
       $value = 'test';
      if($value <> "")
       showmessage("City: ".$City.", ZIP: ".$QRow->ZIP);


Re: Can't figure out the error.

On Wed, 10 Nov 2004 22:07:18 +0100, "Bob Bedford"

Quoted text here. Click to load it

 'a' == 0 is true. This is because of type juggling. See:

 Non-numeric strings implicitly convert to zero integers. When trying to
compare a string with an integer, the left hand expression is converted to the
type of the right hand expression; 'a' becomes 0, which matches 0.

 If you want to suppress type juggling, use the === operator.

 ==  is "both operand expressions are equivalent when converted to a common

 === is "both operand expressions are equivalent in both type and value"

 Consider the output of:

$values = array('0', '1', 'a', '', 0, 1, null, false, true);

for ($i=0; $i<count($values); $i++)
    for ($j=0; $j<count($values); $j++)
        if ($i != $j)
            print "<tr><td>";
            print "</td><td>==</td><td>";
            print "</td><td>";
            print "is ";
            var_dump($values[$i] == $values[$j]);
            print "</td><td>&nbsp;</td>";

            print "<td>";
            print "</td><td>===</td><td>";
            print "</td><td>";
            print "is ";
            var_dump($values[$i] === $values[$j]);
            print "</td></tr>";


Quoted text here. Click to load it

 You said:

Quoted text here. Click to load it

 But the line above is the only time $value is written to, and you put 'test'
into it. So if $value becomes 'a' it's being done by code you haven't posted.

< Space: disk usage analysis tool

Site Timeline