Jump to content

Problem with if -


wildmurphy

Recommended Posts

Hi Guys, I am having an issue with an if statement, I cant get it to go true, even though it clearly is!

 

Please excuse the messy code, im pulling my hair out here!

 

$itemprice[0] = 8.99;

$itemprice[1] = 19.95;

$itemprice[2] = 8.99;

print_r($itemprice);

echo "<BR>";

$x=0;

foreach($itemprice AS $val) {

echo gettype($val) .$val. "<br>";

if($val == 8.99) {

$x++;

$freecount++;

}

}

echo $x;

 

outputs correctly:

Array ( [0] => 8.99 [1] => 19.95 [2] => 8.99 )

double8.99

double19.95

double8.99

x = 2

 

replacing:

$newtotal = add_to_price($id, $non_disc);

$itemprice[] = $newtotal - $non_disc;

print_r($itemprice);

echo "<BR>";

$x=0;

foreach($itemprice AS $val) {

echo gettype($val) .$val. "<br>";

if($val == 8.99) {

$x++;

$freecount++;

}

}

echo $x;

 

The add to price function returns a running total, the take away gives me the current item price in a for loop to build the array.

 

Output:

 

Array ( [0] => 8.99 [1] => 19.95 [2] => 8.99 )

double8.99

double19.95

double8.99

x = 1

 

The problem? Well when I build the array up automatically it still prints the same, has the same datatype, yet the if statement does not catch the second 8.99 value?

 

Please accept my apologies for this post, it is my first ever and i've been doing this for 10 years now, i have never been this stumped on something so simple, I can only think it is a datatype error but i have tried apostrophes in the if, make no difference.

 

All help massively gratefully received.

 

Please email me if you want to see it in action...

Link to comment
Share on other sites

Thanks thorpe, yes i do thanks.

 

Please accept my apologies for this non indent on the forum, my indents looked a bit wide so i tried to smarten it up on here and it went wrong.

 

The problem as it turned out was that the datatype coming back from my fuction was changing and this was not carried through to the array. Even though they look the same before the if test when print_r and gettype were echoed they apparently were not.

 

Not something I have ever experienced in php, its usually good at sorting out its own datatypes.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.