Page 6 of 6

Re: Undefined variable - why am I getting these?

Posted: Tue Apr 08, 2014 9:00 am
by Celauran
Yes. You're checking if the variable exists before trying to use it, the notices go away.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 4:19 am
by simonmlewis

Code: Select all

$combined = ($price1 + $price2 + $price3 + $price4 + $price5);
It's erroring on $price4. Basically because $price4 and $price5 are not always in use.

So how do I do this calculation, *properly*?

I'm assuming I don't do it by putting an "isset" within each section of the query.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 4:27 am
by simonmlewis

Code: Select all

if (isset($bundle1) || isset($bundle2)  || isset($bundle3)  || isset($bundle4)  || isset($bundle5))
  { 
  if ($bundle1 == "soldout" || $bundle2 == "soldout" || $bundle3 == "soldout" || $bundle4 == "soldout" || $bundle5 == "soldout")
}
What is the best way to do this?

I need to check if a variable is "soldout" and then proceed from there. It's erroring on this because perhaps $bundle4 is NULL, and I am then asking about it's contents.

Where have I gone wrong on this?
I need to know if either of those five $bundle* variables has "soldout" in them.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 6:39 am
by Celauran
simonmlewis wrote:

Code: Select all

$combined = ($price1 + $price2 + $price3 + $price4 + $price5);
It's erroring on $price4. Basically because $price4 and $price5 are not always in use.

So how do I do this calculation, *properly*?

I'm assuming I don't do it by putting an "isset" within each section of the query.
No. Initialize your variables; set them all to zero and overwrite the zeroes as appropriate.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 6:41 am
by Celauran
simonmlewis wrote:

Code: Select all

if (isset($bundle1) || isset($bundle2)  || isset($bundle3)  || isset($bundle4)  || isset($bundle5))
  { 
  if ($bundle1 == "soldout" || $bundle2 == "soldout" || $bundle3 == "soldout" || $bundle4 == "soldout" || $bundle5 == "soldout")
}
What is the best way to do this?

I need to check if a variable is "soldout" and then proceed from there. It's erroring on this because perhaps $bundle4 is NULL, and I am then asking about it's contents.

Where have I gone wrong on this?
I need to know if either of those five $bundle* variables has "soldout" in them.
Your first if statement should be using 'and' operators, not 'or'. You need for them all to be set before you can start checking their values.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 6:43 am
by simonmlewis
I think the answer to the second issue is similar to the first.
I have to $bundle1-5 to 0 at the start, and then do the AND in the query.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 7:00 am
by simonmlewis
Even tho $bundle1-5 are set to "0", and on a page I am looking at they all echo as zero, it's coming up as "Bundle sold out".

IS it somehow bypassing the "$bundle1 == 'soldout'" and just assigning it as "sold out" because isset($bundle) ???

Code: Select all

 if (isset($bundle1) && isset($bundle2)  && isset($bundle3)  && isset($bundle4)  && isset($bundle5))
  { 
  if ($bundle1 == "soldout" || $bundle2 == "soldout" || $bundle3 == "soldout" || $bundle4 == "soldout" || $bundle5 == "soldout")
  
  { echo "<input type=button value='Bundle Sold Out' class='submit_buynow' disabled>";
  
  
  if (isset($bundle1)) { if ($bundle1 == "soldout") { $bundle1sold = $row->bundleroman1;} }
  if (isset($bundle2)) { if ($bundle2 == "soldout") { $bundle2sold = $row->bundleroman2;} }
  if (isset($bundle3)) { if ($bundle3 == "soldout") { $bundle3sold = $row->bundleroman3;} }
  if (isset($bundle4)) { if ($bundle4 == "soldout") { $bundle4sold = $row->bundleroman4;} }
  if (isset($bundle5)) { if ($bundle5 == "soldout") { $bundle5sold = $row->bundleroman5;} }  
  } 

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 7:08 am
by Celauran
simonmlewis wrote:Even tho $bundle1-5 are set to "0", and on a page I am looking at they all echo as zero, it's coming up as "Bundle sold out".

IS it somehow bypassing the "$bundle1 == 'soldout'" and just assigning it as "sold out" because isset($bundle) ???
No. It's first checking that they're all set. It then checks if any of them is sold out and displays the Bundle Sold Out button.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 7:19 am
by simonmlewis
Well if I put echo "$bundle1, $bundle2....." just inside that existing echo, it shows all zeros. None show "soldout".
So why is it echoing it at all?

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 7:20 am
by simonmlewis

Code: Select all

if (isset($bundle1) && isset($bundle2)  && isset($bundle3)  && isset($bundle4)  && isset($bundle5))
  { 
  if ($bundle1 == "soldout" || $bundle2 == "soldout" || $bundle3 == "soldout" || $bundle4 == "soldout" || $bundle5 == "soldout")
  
  { echo "bundle1 $bundle1<br/>
  bundle2 $bundle2<br/>
  bundle3 $bundle3<br/>
  bundle4 $bundle4<br/>
  bundle5 $bundle5<br/><input type=button value='Bundle Sold Out' class='submit_buynow' disabled>";
  
  
  if (isset($bundle1)) { if ($bundle1 == "soldout") { $bundle1sold = $row->bundleroman1;} }
  if (isset($bundle2)) { if ($bundle2 == "soldout") { $bundle2sold = $row->bundleroman2;} }
  if (isset($bundle3)) { if ($bundle3 == "soldout") { $bundle3sold = $row->bundleroman3;} }
  if (isset($bundle4)) { if ($bundle4 == "soldout") { $bundle4sold = $row->bundleroman4;} }
  if (isset($bundle5)) { if ($bundle5 == "soldout") { $bundle5sold = $row->bundleroman5;} }  
  } 
This shows bundle1 0
bundle2 0
and so on.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 8:28 am
by Celauran
You'll need to use triple equals here, or initialize your bundles to empty strings. Somehow 0 == 'soldout' is true.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 8:32 am
by simonmlewis
Sorry you've me a bit here.
I'm assigning $bundle as 0.
Then it's asking if $bundle exists.
If any of them do, it then asks if they are "soldout". If none of them are, but they are all 0, it still assumes they are "soldout".

That's illogical?!?!

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 8:36 am
by Celauran
:shrug:

I didn't write PHP. Try it for yourself.

Code: Select all

var_dump(0 == 'soldout');

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 03, 2014 8:37 am
by Celauran
Like I said, either initialize them to empty strings or use a strict comparison.