Variables Not Working -- Help?

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

Post Reply
uemtux
Forum Newbie
Posts: 8
Joined: Fri Jan 22, 2010 9:16 am

Variables Not Working -- Help?

Post 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?
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Variables Not Working -- Help?

Post by aravona »

try this:

include("cart-functions.php");
uemtux
Forum Newbie
Posts: 8
Joined: Fri Jan 22, 2010 9:16 am

Re: Variables Not Working -- Help?

Post by uemtux »

Thanks for the very quick response!

However it didn't work, same errors...
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Variables Not Working -- Help?

Post 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");
uemtux
Forum Newbie
Posts: 8
Joined: Fri Jan 22, 2010 9:16 am

Re: Variables Not Working -- Help?

Post 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...
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Variables Not Working -- Help?

Post 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.
uemtux
Forum Newbie
Posts: 8
Joined: Fri Jan 22, 2010 9:16 am

Re: Variables Not Working -- Help?

Post 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.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Variables Not Working -- Help?

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Variables Not Working -- Help?

Post by pickle »

Echo those variable right after you create them, and right before you pass them as parameters.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
uemtux
Forum Newbie
Posts: 8
Joined: Fri Jan 22, 2010 9:16 am

Re: Variables Not Working -- Help?

Post 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!
Post Reply