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

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 »

So how do I get around it?
The page is designed where the person reaches the page and is then asked to enter a price range.
I would have put this into a script that only uses it when something is posted, but that is what the SESSION is for, so it can go page by page.
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 »

Code: Select all

$pricemin = 0;
if (isset($_POST['pricemin'])) {
	$pricemin = $_POST['pricemin'];
} else if (isset($_SESSION['pricemin'])) {
	$pricemin = $_SESSION['pricemin'];
}

// Now let's store it in session data
$_SESSION['pricemin'] = $pricemin;
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

$loadout_recalc = $price1calc + $price2calc + $price3calc + $price4calc + $price5calc + $price6calc + $price7calc + $price8calc + $price9calc + $price10calc;
I am getting an error on this - the usual:

[text]Notice: Undefined variable: price8calc in C:\xampp\phpMyAdmin\site\includes\loadout.inc on line 1135[/text]

Clearly it's because $price8calc doesn't have anything in it. But how do I do that calculation, without getting that error?
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 »

Initialize them all with a value of 0?
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 »

Blimey it's obvious when someone tells you!!
Thanks.
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 (is_null($row->doc1) && ($row->doc2) && ($row->doc3))
       {  echo " color:#ff0000";}
What's wrong with this?
The three fields all are set to NULL. So I thought the text should be shown as RED.
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're only checking if the first one is null
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 (is_null($row->doc1) && is_null($row->doc2) && is_null($row->doc3))
       {  echo " color:#ff0000";}
??
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 »

:) Cool - it works. Ta.
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 »

What is wrong with this?

Code: Select all

if (strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')!==FALSE) {
echo "<div style='clear: both;' /><br/><img src='/images/divider.png' /></div>";}
      elseif (strpos($_SERVER['HTTP_USER_AGENT'],'Safari')!==FALSE) {
echo "<div style='clear: both;' /><br/><img src='/images/divider.png' /></div>";}
elseif (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')!==FALSE) {
echo "<div style='clear: both;' /><br/><img src='/images/divider_ie.png' /></div>";}
else { echo "<div style='clear: both;' /><br/><img src='/images/divider_nogap.png' /></div>";}
We are getting dozens of errors like this:
[text][Mon Apr 07 17:41:30 2014] [error] [client 50.28.34.45] PHP Notice: Undefined index: HTTP_USER_AGENT in /var/www/vhosts/site.co.uk/httpdocs/includes/categ.inc on line 413[/text]
Line 413 is is the top line of that.

We are getting hundreds of these errors on other pages that use the same format of code.
Is that now incorrect coding to capture the user agent? Something is causing a massive amount of these errors.
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 »

It's not a required header, so it's possible not all requests contain it. Like all undefined index notices, just check if the array key exists before trying to use it.
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 how do you mean? I need to see what the browser is.
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 »

Any browser or agent can choose not to send that information, so it may not always be set, and you may not always be able to tell what browser is making the current request. Nothing you can do about that.
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 »

Gotchya.
So the large amount of errors we are now getting for this could be someone who is blocking it, or jsut using a browser that won't idenfity it. So I need to put the query into a variable and query the variable instead? if it is isset...??
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 »

Actually, is this sufficient?

Code: Select all

      $browsercheck = $_SERVER['HTTP_USER_AGENT'];
      if(isset($browsercheck))
      {
      if (strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')!==FALSE) {
echo "<div style='clear: both;' /><br/><img src='/images/divider.png' /></div>";}
      elseif (strpos($_SERVER['HTTP_USER_AGENT'],'Safari')!==FALSE) {
echo "<div style='clear: both;' /><br/><img src='/images/divider.png' /></div>";}
elseif (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')!==FALSE) {
echo "<div style='clear: both;' /><br/><img src='/images/divider_ie.png' /></div>";}
else { echo "<div style='clear: both;' /><br/><img src='/images/divider_nogap.png' /></div>";}
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply