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

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:What if there is a block?
Its still being set at zero anyway.
It does work as we see results from it.
Denial is not a programming paradigm... ;)
(#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 »

You don't even need $count = 0; do you?
Because: $count = $count + 1; sets it - doesn't it?
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 »

And again: what does $count = unassigned + 1 equal? Obviously in our time/space continuum the addition has to happen before the assignment. Right?
(#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 »

Ok, so $count is set to zero.
I then tell it $count (which is zero) is $count (which is zero) + 1.

Nothing else in this page is happening to $count before this, so again, why is it saying it is undefined?
Guess I'll never know.
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 »

simonmlewis wrote:Ok, so $count is set to zero.
No. No it isn't. I can't tell you exactly why because you refuse to show your code, but it very clearly isn't being set to zero or you would not be getting a notice telling you it's undefined. This really ought to be abundantly clear.
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 »

Ok - $count is not mentioned anywhere else in the code.
$count = 0; is set immediately before the $count + 1 script.

There is no other way I can tell it to be zero. I've even told $count = 0; right at the very top of the page, so there is no question or doubt.
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 »

Debugging is not about insisting that the PHP parser is wrong. It's about finding where $count is unset. Also, are you positive that the line you are looking at is the line that PHP is complaining about?
(#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 »

100%
Is there a Firefox way to view what is happening and see if $count is somehow (heaven knows how as it's mentioned ONCE) being ignored/uninitialised? Error Console??
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 »

PHP is server side, so you can't really do anything to it in your browser. You could try stepping through the code with XDebug.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Undefined variable - why am I getting these?

Post by mikosiko »

a very simple test will be replace this line:

Code: Select all

$count = $count + 1;
for this one

Code: Select all

$count++;
and see if you still getting the message... I'm guessing that you could potentially have a hidden character in the original line that could be the culprit.
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->bundleroman1 == NULL || $row->bundleroman1 == "")
{ echo "<br/>$row->rcstock";}
This is wrong apparently, so how do I redo this?

Code: Select all

if(empty($row->bundleroman1))
{ echo "<br/>$row->rcstock";
Like this?
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

if ($row->bundleroman1 == NULL || $row->bundleroman1 == "")
{ echo "<br/>$row->rcstock";}
This is wrong apparently, so how do I redo this?
It depends on what you are checking for? What value does $row->bundleroman1 have when it is valid?
simonmlewis wrote:

Code: Select all

if(empty($row->bundleroman1))
{ echo "<br/>$row->rcstock";
Like this?
That might be one way. See the documentation for the difference between isset() and empty().
(#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 »

If it is not empty, it will have someting like "XLGREEN2". A stock code.
ISSET means it has something in it. EMPTY means it's empty.
Does it not?
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

$var = 0;
var_dump(empty($var));
var_dump(isset($var));
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 »

Also, take a look at: http://php.net/manual/en/types.comparisons.php

You need to take a look at the bigger picture. The value should be properly initialized so you can do a clean check on it.
(#10850)
Post Reply