-
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?
-
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;
}
-
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;
}
-
Thanks. Chris, that's how I'm currently handling it. Martin, I'll give that a try when I get the chance. Thanks alot :)