Page 1 of 1
comparing two variables - 2==0?
Posted: Wed May 31, 2006 10:54 pm
by galiwackitz
hello. i am simply trying to compare two variables, but i am not getting the expected results.
first variable - i set explicitly:
$thecounter = 0;
the second variable comes from a form submission. it's called $numberOfItems, and when I print it out, it displays as 2 (the number two).
i even go through the step of:
settype($numberofitems, "integer");
settype($thecounter, "integer");
but when i compare the two, it tells me that they're equal.
also note that if i set $counter to 1 (one), it says that counter is greater than $numberOfItems - so i guess this means that it's a type issue with $numberOfItems? php is converting it to a zero somewhere?
your time is much appreciated.
chris
Posted: Wed May 31, 2006 11:09 pm
by Christopher
Please post the code that you are having a problem with.
code snippet
Posted: Wed May 31, 2006 11:38 pm
by galiwackitz
code excerpts are below:
note:
$vars is the array of posted form variables...
arborint | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Code: Select all
$vars = array("oppNumber", "accNumber", "oppDescription", "numberOfItems",
"productName0", "productDescription0", "T3_Line_Specs__c0", "quantity0",
"subtotal", "total");
$thecounter = 0;
$outputStr = $outputStr . "<tr><td>numberofitems:$numberOfItems</td></tr>";
$outputStr = $outputStr . "<tr><td>thecounter:$thecounter</td></tr>";
if ($thecounter < $numberOfItems) {
$outputStr = $outputStr . "<tr><td>less than</td></tr>";
}
if ($thecounter > $numberOfItems) {
$outputStr = $outputStr . "<tr><td>greater than</td></tr>";
}
if ($thecounter == $numberOfItems) {
$outputStr = $outputStr . "<tr><td>equal</td></tr>";
}
if ($thecounter != $numberOfItems) {
$outputStr = $outputStr . "<tr><td>not equal</td></tr>";
}
arborint | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
when i run this code, i see the following:
numberofitems:2
thecounter:0
equal
why are they equal? i've tried converting to strings - and that works for the first pass. but when i increment the counter, the comparison no longer works.
if it matters, i'm running php5 on a windows xp box. called from apache2.
thanks
chris
Posted: Thu Jun 01, 2006 10:21 pm
by galiwackitz
thanks for the tips, and my apologies for the bad syntax.
fyi, still seeing the issue - not able to get around it. any insight is appreciated.
chris
Posted: Thu Jun 01, 2006 10:25 pm
by Burrito
try echoing out your post val to make sure that it's what you think it is.
you can also try using
intval() to convert your post var to an integer.
Posted: Sun Jun 04, 2006 8:52 pm
by galiwackitz
i'm still no having any luck with this. what should i do to diagnose the real issue? i checked the type of the posted param and it tells me it's a string (is_string is true). but i should be able to compare a string with a value of 2 to an integer, right (using ==, but not ===)? the docs tell me that this isn't a problem. any other suggestions?
thanks for your time.
chris
Posted: Sun Jun 04, 2006 9:31 pm
by Christopher
I don't see in the code you posted where the value for $numberOfItems is set. Can you post that code.
Posted: Mon Jun 05, 2006 5:49 am
by galiwackitz
My mistake. I posted the array, but not the POST info. It's all below:
Code: Select all
$vars = array("oppNumber", "accNumber", "oppDescription", "numberOfItems",
"subtotal", "total");
foreach ($vars as $var)
$$var = "<b>" . str_replace("\n", "<br />\n", htmlspecialchars($_POST[$var])) . "</b>";
And as I mentioned, I echo the value and I get: 2. Seems simple enough.
thanks again
chris
Posted: Mon Jun 05, 2006 5:56 am
by twigletmac
Try using
trim() on all the posted data to remove any unexpected whitespace - just in case there's a rogue space or something around the number. Also, on a side note, there's an easier way to add break tags by using
nl2br(). So you can replace:
Code: Select all
$$var = "<b>" . str_replace("\n", "<br />\n", htmlspecialchars($_POST[$var])) . "</b>";
with
Code: Select all
$$var = "<b>" . htmlspecialchars(nl2br(trim($_POST[$var]))) . "</b>";
Mac