Page 4 of 6
Re: Undefined variable - why am I getting these?
Posted: Fri Aug 16, 2013 3:07 am
by simonmlewis
So if a field is set to NULL, then it can be "IS NULL", but if it is not, then it "EMPTY" ?
Re: Undefined variable - why am I getting these?
Posted: Fri Aug 16, 2013 2:34 pm
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?
Re: Undefined variable - why am I getting these?
Posted: Mon Aug 19, 2013 3:14 am
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.
Re: Undefined variable - why am I getting these?
Posted: Mon Aug 19, 2013 5:52 am
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.
Re: Undefined variable - why am I getting these?
Posted: Mon Aug 19, 2013 6:41 am
by Celauran
preg_replace returns null if there's an error, which would leave those variables undefined.
Re: Undefined variable - why am I getting these?
Posted: Thu Aug 22, 2013 2:44 am
by akhilesh1010
You should use isset function before use any variable.
Re: Undefined variable - why am I getting these?
Posted: Thu Sep 12, 2013 3:52 pm
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.
Re: Undefined variable - why am I getting these?
Posted: Fri Sep 27, 2013 4:09 am
by simonmlewis
How do you do multiple of these?
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.
Re: Undefined variable - why am I getting these?
Posted: Fri Sep 27, 2013 4:20 am
by simonmlewis
Re: Undefined variable - why am I getting these?
Posted: Thu Nov 07, 2013 4:43 am
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?
Re: Undefined variable - why am I getting these?
Posted: Thu Nov 07, 2013 5:37 am
by Celauran
Check that $_SESSION['catid'] is set.
Re: Undefined variable - why am I getting these?
Posted: Thu Nov 07, 2013 5:40 am
by simonmlewis
Is that not:
??
If not, what should I be putting?
Re: Undefined variable - why am I getting these?
Posted: Thu Nov 07, 2013 5:46 am
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'];
}
Re: Undefined variable - why am I getting these?
Posted: Thu Jan 23, 2014 11:09 am
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.
Re: Undefined variable - why am I getting these?
Posted: Thu Jan 23, 2014 11:11 am
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.