comparing two variables - 2==0?
Moderator: General Moderators
-
galiwackitz
- Forum Newbie
- Posts: 5
- Joined: Wed May 31, 2006 10:39 pm
comparing two variables - 2==0?
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
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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
-
galiwackitz
- Forum Newbie
- Posts: 5
- Joined: Wed May 31, 2006 10:39 pm
code snippet
code excerpts are below:
note:
$vars is the array of posted form variables...
arborint | Please use
arborint | Please use
note:
$vars is the array of posted form variables...
arborint | Please use
Code: Select all
,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
,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-
galiwackitz
- Forum Newbie
- Posts: 5
- Joined: Wed May 31, 2006 10:39 pm
-
galiwackitz
- Forum Newbie
- Posts: 5
- Joined: Wed May 31, 2006 10:39 pm
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
thanks for your time.
chris
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
-
galiwackitz
- Forum Newbie
- Posts: 5
- Joined: Wed May 31, 2006 10:39 pm
My mistake. I posted the array, but not the POST info. It's all below:
And as I mentioned, I echo the value and I get: 2. Seems simple enough.
thanks again
chris
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>";thanks again
chris
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
with
Mac
Code: Select all
$$var = "<b>" . str_replace("\n", "<br />\n", htmlspecialchars($_POST[$var])) . "</b>";Code: Select all
$$var = "<b>" . htmlspecialchars(nl2br(trim($_POST[$var]))) . "</b>";