Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Global variables not passed into a function
Okay, I am developing a plugin (for multilingual posts). As yet I have the PHP code pasted into the default template (under “Presentation”) and not installed in ‘plugin cache dir’ as a plugin. Everything executes as it should, but I cannot get variables to be passed into the function, although register_globals is ‘on’ on my server.
Here is the code (part of it) as it should be:
$selected_language = ‘en’;
function choose_language($content) { global $selected_language;
//here is the code to extract the part of the post in the selected language
// but for testing:
echo $selected_language; // returns nothing !
}
If I define $selected_language inside the function, the rest of the code (not displayed here) works fine, and if I do not define and call a function but directly put the selector code where I need it, it works as well.
I just cannot pass the variable from outside the function to the inside.
What am I doing wrong?
Offline
#2 2006-01-24 10:53:29
- zem
- Developer Emeritus
- From: Melbourne, Australia
- Registered: 2004-04-08
- Posts: 2,579
Offline
Re: Global variables not passed into a function
Hmm…,
while <code><txp:php> echo “Hello world”; </txp:php></code> works fine on my page, my own code does not execute if I include it in <code><txp:php> … </txp:php></code> instead of <code><?php … ?></code> (with which it works!).
Why?
—-
After reading the FAQ and playing with my code, I seem to have found the solution to my question:
Declaring the global variable outside the function did the trick.
Again: Why?
—-
Anyway, thanks, Zem, for helping me along the way.
Last edited by Manfred Kooistra (2006-01-24 11:35:21)
Offline
#4 2006-01-24 21:38:10
- zem
- Developer Emeritus
- From: Melbourne, Australia
- Registered: 2004-04-08
- Posts: 2,579
Re: Global variables not passed into a function
As the FAQ says: “your PHP code will not be executed in the global scope, so you’ll need to explicitly declare global variables as required.”
The first time you used $selected_language
, you assumed it was already in the global scope. It wasn’t.
Alex
Offline