PDA

Click to See Complete Forum and Search --> : Variables


The Hobo
May 11th, 2002, 10:29 PM
I'm pretty sure someone has asked this before, but lets say I have this:


$title = "My Title";

if ($action == 'view') {
view();
elseif ($action == 'show') {
show();
}

function view() {
echo $title;
}

function show() {
echo $title;
}


Nothing shows up?

chrisjk
May 11th, 2002, 10:53 PM
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

$title = "My Title";

if ($action == 'view') {
view($title);
elseif ($action == 'show') {
show($title);
}

function view($title) {
echo $title;
}

function show($title) {
echo $title;
}

Martin Wilson
May 12th, 2002, 05:31 AM
Or you can make the variable global:$title = "My Title";

if ($action == 'view') {
view();
elseif ($action == 'show') {
show();
}

function view() {
global $title;
echo $title;
}

function show() {
global $title;
echo $title;
}

The Hobo
May 13th, 2002, 11:13 AM
Thanks. Chris, that's how I'm currently handling it. Martin, I'll give that a try when I get the chance. Thanks alot :)