By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Can't figure out the error.

P: n/a
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';
}
echo(">".$QRow->ZIP."&nbsp;".$QRow->Cityname."</OPTION>\n");
if($value <> "")
showmessage("City: ".$City.", ZIP: ".$QRow->ZIP);
}
}

Bob
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Wed, 10 Nov 2004 22:07:18 +0100, "Bob Bedford"
<be******@YouKnowWhatToDoHerehotmail.com> wrote:
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 ????
'a' == 0 is true. This is because of type juggling. See:

http://www.php.net/manual/en/languag...e-juggling.php

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
type"

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

Consider the output of:

<table>
<?php
$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>";
var_dump($values[$i]);
print "</td><td>==</td><td>";
var_dump($values[$j]);
print "</td><td>";
print "is ";
var_dump($values[$i] == $values[$j]);
print "</td><td>&nbsp;</td>";

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

}
}
}
?>
</table>
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';
You said:
When I run the code below, $value becomes "a".


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.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.