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 »

Got a new error here:

Code: Select all

 .....$count = 0;
            if ($num_rcstock == 1)
              {
                  $count = $count + 1;
.....
[Fri Jul 19 10:58:11 2013] [warn] [client 82.110.149.20] mod_fcgid: stderr: PHP Notice: Undefined variable: count in /var/www/vhosts/site.co.uk/httpdocs/includes/product.inc on line 1518, referer: http://www...................
I am assigning zero to $count, and then adding one to $count on each cycle, yet it's telling me it's undefined???
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 »

These lines aren't telling us anything, could you post more of the code? Ideally with line numbers?
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 »

They are actually. Though I failed to tell you which line was the error one.

Code: Select all

 $count = $count + 1;
This is what it doesn't like. I added the $count = 0; because it kept saying $count was undefined.
So I am defining it.

What else does it need??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Undefined variable - why am I getting these?

Post by Christopher »

simonmlewis wrote:

Code: Select all

 $count = $count + 1;
This is what it doesn't like. I added the $count = 0; because it kept saying $count was undefined.
So I am defining it.

What else does it need??
You need to actually think about what is going on in your lines of code. What is the value of $count before you add 1 to it?
(#10850)
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 »

Yes I know.
Surely that is doing BOTH tasks?
$count isn't assigned before. But now I am saying $count is $count + 1.
However in the code I have submitted, it shows $count is 0.
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 ($row->preorder == "yes") { echo "<br/>pre-order only";}
For this, I am getting this error:

[text][Mon Jul 22 14:05:43 2013] [warn] [client 79.78.112.208] mod_fcgid: stderr: PHP Notice: Undefined property: stdClass::$preorder[/text]
Do I have to do:

Code: Select all

if(isset($row->preorder)
{
if ($row->preorder == "yes") { echo "<br/>pre-order only";}
}
For all these as well?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Undefined variable - why am I getting these?

Post by Christopher »

simonmlewis wrote:Yes I know.
Surely that is doing BOTH tasks?
$count isn't assigned before. But now I am saying $count is $count + 1.
And PHP is telling you that it does not know what unassigned+1 is.
simonmlewis wrote:However in the code I have submitted, it shows $count is 0.
The code is cut up, so I can see if the $count=0; code is actually being executed before the assignment.
(#10850)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Undefined variable - why am I getting these?

Post by Christopher »

simonmlewis wrote:[text][Mon Jul 22 14:05:43 2013] [warn] [client 79.78.112.208] mod_fcgid: stderr: PHP Notice: Undefined property: stdClass::$preorder[/text]
Do I have to do:
For all these as well?
Probably not. Where is $row coming from? The error says it is an object of class stdClass. And that it does not have a preorder property. Find the source of $row and find the source of your problem.

Come on Simon. You can find the source of these errors!
(#10850)
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

$result = mysql_query ("SELECT id, code FROM products WHERE id = '$myprod'");
while ($row = mysql_fetch_object($result))
        {
if ($row->preorder != NULL) { echo "Pre order only";}
}
This is basically it, without hundreds of lines of other code, not related to pre order.
So is this incorrect?

$count has right above it:

$count = 0;

I did this when I saw the error, as I thought if it doesn't know what $count is, then I have to tell it that it's a zero, so at least perhaps it knows it's an integer?! And then it adds 1 each time.

I've no idea how this is wrong.
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 »

Think I have sorted the top one - it wasn't extracting those fields from the DB. I don't know if that is the errors, but at least it's now finding the values.

But what about $count?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Undefined variable - why am I getting these?

Post by Christopher »

Have you verified that $count is actually being initialized?
(#10850)
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 »

Bearing in mind I have $count = 0; set, does that not do it?
If not, then how?

This is what I just read, and I've been doing this a long time. So I'm confused.

Code: Select all

$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var"; // outputs "Bob, Joe"
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Undefined variable - why am I getting these?

Post by Christopher »

Code: Select all

 .....$count = 0;
            if ($num_rcstock == 1)
              {
                  $count = $count + 1;
.....
From the code you gave, I can't tell if $count is being initialized or not. It could be in an if() block that is not run.
[Fri Jul 19 10:58:11 2013] [warn] [client 82.110.149.20] mod_fcgid: stderr: PHP Notice: Undefined variable: count in /var/www/vhosts/site.co.uk/httpdocs/includes/product.inc on line 1518, referer: http://www...................
Also, are you positive that the code above is line 1518?
(#10850)
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 if there is a block?
Its still being set at zero anyway.
It does work as we see results from it.
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 »

If it were being set to zero, you wouldn't be getting the undefined notice.
Post Reply