Re: Undefined variable - why am I getting these?
Posted: Tue Apr 08, 2014 9:00 am
Yes. You're checking if the variable exists before trying to use it, the notices go away.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$combined = ($price1 + $price2 + $price3 + $price4 + $price5);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")
}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.
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.
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;} }
} 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) ???
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;} }
}