Undefined variable - why am I getting these?
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?
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.
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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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?
Code: Select all
$loadout_recalc = $price1calc + $price2calc + $price3calc + $price4calc + $price5calc + $price6calc + $price7calc + $price8calc + $price9calc + $price10calc;[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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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?
Blimey it's obvious when someone tells you!!
Thanks.
Thanks.
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 (is_null($row->doc1) && ($row->doc2) && ($row->doc3))
{ echo " color:#ff0000";}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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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?
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.
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?
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?
What is wrong with this?
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.
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>";}[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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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?
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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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?
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...??
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.
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?
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.
All the best from the United Kingdom.