Page 1 of 1

Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 9:21 am
by uemtux
Hi all, I have a kind of mysterious problem, I hope you all can help me. Here's the PHP file I am loading in the browser:

Code: Select all

<?php
error_reporting(E_ALL); 
ini_set("display_errors", 1); 
 
include 'cart-functions.php';
 
$displaytitles = 40;
$displaypage = 1;
$searchstring = "";
 
print writeShoppingCartJ();
drawstore_pageline_search($displaytitles, $displaypage, $searchstring);
drawstore_horiz_searchC($displaytitles, $displaypage, $searchstring);
 
?>
Notice: Undefined variable: displaytitles in custom-store.php on line 12
Notice: Undefined variable: displaypage in custom-store.php on line 12
Notice: Undefined variable: searchstring in custom-store.php on line 12
Notice: Undefined variable: displaytitles in custom-store.php on line 13
Notice: Undefined variable: displaypage in custom-store.php on line 13
Notice: Undefined variable: searchstring in custom-store.php on line 13

I get these errors when loading the page, and the arguments do not seem to be passed on to the functions, yet if I replace the variables in the function argument with plain numbers, the functions work exactly as expected.

What the heck is going on?

Re: Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 9:23 am
by aravona
try this:

include("cart-functions.php");

Re: Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 9:25 am
by uemtux
Thanks for the very quick response!

However it didn't work, same errors...

Re: Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 9:28 am
by aravona
try then naming your file .inc this may work (basing it on an old project of mine which had include files.)

include("cart-functions.inc");

Re: Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 9:33 am
by uemtux
Same errors with the .inc file extension....

Another thing, don't know if this helps, variables still work... like, I can:

echo $displaytitles;

and I'll get a 40 as expected...

Re: Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 9:37 am
by timWebUK
It's nothing to do with include file...

I think it's saying there are undefined variables, meaning for some reason those functions can't see the variables you're passing as parameters.

Re: Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 9:40 am
by uemtux
Yeah, but why? It shouldn't be an issue of scope, the include file has a bunch of functions in it, that's it, the variables are defined in the same file as the function calls...

So what the heck? I've been writing PHP for years, I've never seen this before.

Re: Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 9:42 am
by aravona
sorry, a little tired, i read,
Notice: Undefined variable: displaytitles in custom-store.php on line 12
as
Notice: Undefined variable: displaytitles in cart-functions.php on line 12

Sorry!

Ok so this isnt an error. its a notice just saying that your code is possibly wrong. Unfortunately its aying you havent defined your variables, when obviously you have.
So what the heck? I've been writing PHP for years, I've never seen this before.
ive seen it and fixed it but for the life of me i cant remember how. I'll have a think.

Re: Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 10:00 am
by pickle
Echo those variable right after you create them, and right before you pass them as parameters.

Re: Variables Not Working -- Help?

Posted: Fri Jan 22, 2010 10:06 am
by uemtux
Turns out I wasn't paying attention... the first function I call writeShoppingCartJ(); starts a session, so the variables are defined outside the session so of course, they don't work. Basically I've gotta re-write my functions, I'm converting a shopping cart I wrote to work within Joomla to work as standalone php and I guess I got lazy... :)

Thanks everyone for your time!