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 if a field is set to NULL, then it can be "IS NULL", but if it is not, then it "EMPTY" ?
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 »

If a variable is undefined or set to null, then empty() or is_null() will return TRUE while isset() will return FALSE. Did you look at the comparisons chart in the link I put in the previous post?
(#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. But I don't understand a lot of it. Even I have done this for years, I never knew there we so many different types: empty, is_null, isset and so on.
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

$title = "$row->title"; 
 $findtitle ="/ /"; 
 $replacetitle ="-"; 
 $titlereplace = preg_replace ($findtitle, $replacetitle, $title); 

 $categ = "$row->catname"; 
 $findcateg ="/ /"; 
 $replacecateg ="-"; 
 $categreplace = preg_replace ($findcateg, $replacecateg, $categ); 

 $subcateg = "$row->subname"; 
 $findsubcateg ="/ /"; 
 $replacesubcateg ="-"; 
 $subcategreplace = preg_replace ($findsubcateg, $replacesubcateg, $subcateg); 
Here are variables clearly set. Using them to do the mod-rewrite.
But it's saying $titlereplace, $categreplace and $subcategreplace are all undefined.
[Mon Aug 19 04:14:11 2013] [error] [client 157.55.32.84] PHP Notice: Undefined variable: titlereplace in /var/www/vhosts/site/httpdocs/includes/product.inc on line 1115
1115 is :

Code: Select all

<a href='/product/$row->catid/$categreplace/$row->subid/$subcategreplace/$row->id/$titlereplace' style='text-decoration: none'>
How else do you define a variable to use it, other than how I have done? These variables will always be in the script,
This A HREF will not always be there, but the variables at the top will. So I am confused yet again.
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 »

preg_replace returns null if there's an error, which would leave those variables undefined.
akhilesh1010
Forum Newbie
Posts: 15
Joined: Thu Aug 22, 2013 1:56 am

Re: Undefined variable - why am I getting these?

Post by akhilesh1010 »

You should use isset function before use any variable.
irony09
Forum Newbie
Posts: 2
Joined: Sun Mar 25, 2012 9:48 am

Re: Undefined variable - why am I getting these?

Post by irony09 »

Here´s a good tool for debugging that can watch for a variable when it changes inside the code: http://phptoolcase.com/guides/ptc-debug-guide.html.
It can also dump variables in a more friendly format then var_dump. It also sends the log to the js console with Chrome browser phpconsole addon installed.
Apart from the weak css, I must say I was impressed by this tool and I advice 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 »

How do you do multiple of these?

Code: Select all

if ($isset($specialcode)) { }
At the moment I am doing this:

Code: Select all

if ($specialcode != NULL && $specialcode2 != NULL && $specialcode3 != NULL) { }
I don't want to do lots of individual ISSETs, as at the end of it there is just one conclusion. So don't want to show five conclusions. It has to be if all 3 are not NULL.
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 »

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(isset($_POST['catid']))
{
    $catid = $_POST['catid'];
    $_SESSION['catid']=$catid;
} else { $catid=$_SESSION['catid'];
}
When the page first loads, I get this:
[text]Notice: Undefined index: catid in C:\xampp\phpMyAdmin\site\includes\sell.inc on line 43
[/text]
Why? How do I resolve 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 »

Check that $_SESSION['catid'] is set.
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 »

Is that not:

Code: Select all

$_SESSION['catid']=$catid;
??
If not, what should I be putting?
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 »

That's inside an if block that only runs if $_POST['catid'] is set. If not, $_SESSION['catid'] could remain undefined. Why not set it to a default value, then adjust it if it has been set elsewhere?

Code: Select all

$catid = 1; // Default
if (isset($_POST['catid'])) {
    $catid = $_POST['catid'];
    $_SESSION['catid'] = $_POST['catid'];
} else if (isset($_SESSION['catid'])) {
    $catid = $_SESSION['catid'];
}
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(isset($_POST['pricemin']))
{
    $pricemin = $_POST['pricemin'];
    $_SESSION['pricemin']=$pricemin;
} else {
    $pricemin=$_SESSION['pricemin'];
}
The page loads with this, and a few others like it.
I get the error:
[text]Notice: Undefined index: pricemin in C:\xampp\phpMyAdmin..............[/text]

I've even tried to say, if none of the above are true, then $pricemin = NULL;. Error remains.
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 »

In the code you posted above, if $_POST['pricemin'] isn't set and $_SESSION['pricemin'] isn't set, you'll get that notice.
Post Reply