Undefined variable - why am I getting these?
Moderator: General Moderators
Re: Undefined variable - why am I getting these?
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?
Code: Select all
$combined = ($price1 + $price2 + $price3 + $price4 + $price5);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.
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?
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")
}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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
No. Initialize your variables; set them all to zero and overwrite the zeroes as appropriate.simonmlewis wrote:It's erroring on $price4. Basically because $price4 and $price5 are not always in use.Code: Select all
$combined = ($price1 + $price2 + $price3 + $price4 + $price5);
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?
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 wrote:What is the best way to do this?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") }
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.
-
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?
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.
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.
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?
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) ???
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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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 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) ???
-
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?
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?
So why is it echoing it at all?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
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?
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;} }
} bundle2 0
and so on.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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?
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?!?!
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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
Like I said, either initialize them to empty strings or use a strict comparison.