Undefined variable - why am I getting these?

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

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined variable - why am I getting these?

Post by Celauran »

Yes. You're checking if the variable exists before trying to use it, the notices go away.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined variable - why am I getting these?

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined variable - why am I getting these?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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;} }  
  } 
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined variable - why am I getting these?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined variable - why am I getting these?

Post by Celauran »

You'll need to use triple equals here, or initialize your bundles to empty strings. Somehow 0 == 'soldout' is true.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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?!?!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined variable - why am I getting these?

Post by Celauran »

:shrug:

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

Code: Select all

var_dump(0 == 'soldout');
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined variable - why am I getting these?

Post by Celauran »

Like I said, either initialize them to empty strings or use a strict comparison.
Post Reply