comparing two variables - 2==0?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
galiwackitz
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 10:39 pm

comparing two variables - 2==0?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Please post the code that you are having a problem with.
(#10850)
galiwackitz
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 10:39 pm

code snippet

Post by galiwackitz »

code excerpts are below:

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

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
galiwackitz
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 10:39 pm

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I don't see in the code you posted where the value for $numberOfItems is set. Can you post that code.
(#10850)
galiwackitz
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 10:39 pm

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply