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 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.
All the best from the United Kingdom.
- 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?
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?
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.
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
$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); But it's saying $titlereplace, $categreplace and $subcategreplace are all undefined.
1115 is :[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
Code: Select all
<a href='/product/$row->catid/$categreplace/$row->subid/$subcategreplace/$row->id/$titlereplace' style='text-decoration: none'>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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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?
You should use isset function before use any variable.
Re: Undefined variable - why am I getting these?
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.
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?
How do you do multiple of these?
At the moment I am doing this:
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.
Code: Select all
if ($isset($specialcode)) { }Code: Select all
if ($specialcode != NULL && $specialcode2 != NULL && $specialcode3 != NULL) { }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?
Code: Select all
if(isset($_POST['catid']))
{
$catid = $_POST['catid'];
$_SESSION['catid']=$catid;
} else { $catid=$_SESSION['catid'];
}[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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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?
Is that not:
??
If not, what should I be putting?
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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
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?
Code: Select all
if(isset($_POST['pricemin']))
{
$pricemin = $_POST['pricemin'];
$_SESSION['pricemin']=$pricemin;
} else {
$pricemin=$_SESSION['pricemin'];
}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.
All the best from the United Kingdom.
Re: Undefined variable - why am I getting these?
In the code you posted above, if $_POST['pricemin'] isn't set and $_SESSION['pricemin'] isn't set, you'll get that notice.