|
-
May 11th, 2002, 10:29 PM
#1
Thread Starter
Stuck in the 80s
Variables
I'm pretty sure someone has asked this before, but lets say I have this:
PHP Code:
$title = "My Title";
if ($action == 'view') {
view();
elseif ($action == 'show') {
show();
}
function view() {
echo $title;
}
function show() {
echo $title;
}
Nothing shows up?
-
May 11th, 2002, 10:53 PM
#2
PowerPoster
variables in functions are considered local to that function, they do not have global scope
you can do this to overcome it, or use the global keyword at the top of the script to change the behaviour
PHP Code:
$title = "My Title";
if ($action == 'view') {
view($title);
elseif ($action == 'show') {
show($title);
}
function view($title) {
echo $title;
}
function show($title) {
echo $title;
}
-
May 12th, 2002, 05:31 AM
#3
Addicted Member
Or you can make the variable global:
PHP Code:
$title = "My Title";
if ($action == 'view') {
view();
elseif ($action == 'show') {
show();
}
function view() {
global $title;
echo $title;
}
function show() {
global $title;
echo $title;
}
What is the answer to this question?
-
May 13th, 2002, 11:13 AM
#4
Thread Starter
Stuck in the 80s
Thanks. Chris, that's how I'm currently handling it. Martin, I'll give that a try when I get the chance. Thanks alot
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|