Forum Moderators: coopster
Did you read the definitions of the scope of variables in PHP ?
or declare the variable global in the parent function
--which kinda defeats the point of having an include file
<?php
// no variable $a defined here
// you could use it, but as long as you don't there's no problem.
function foxtrot() {
global $a;
// can use $a with a global scope
// ...
function golf() {
global $a;
//can access the global variable a (just like it's parent can, so they both can)
}
// ...
}
// ...
function hotel() {
// no global here
$a=1;
// will not interfere with the global variable $a
function juliet() {
global $a;
// can use $a with a global scope, will not interfere with the parent's $a
}
}
?>
Can you slap a Global label on something you haven't defined yet?
$GLOBALS['a'], instead of having to declare
global $ainside the function.
--which kinda defeats the point of having an include fileactually not
I think lucy is a little bit off in understanding the relationship of "included" files to the "top level" script.
Imagine cutting the content of each one of the included files and pasting their content in place of the respective "include" directive that referred to it, to make one very long file with all the code in it.
In PHP <5.3, whilst nested functions are syntactically OK, they aren't really supported and are best avoided if at all possible.
If the outer function is called a second time then the nested function is redeclared and you get a fatal error.
you can define it in those functions that want to use it
In PHP <5.3, whilst nested functions are syntactically OK...
Does "a second time" mean within the same page?
/* FILE: bar.inc.php */
function bar() {
// Do something...
}
function foo() {
include 'bar.inc.php';
echo 'OK';
}
foo(); // "OK"
foo(); // Fatal error: Cannot redeclare bar()...
by the sounds of it you are probably only calling these functions once - to generate your footer(?) - so it's not going to be an issue.
The following would cause a problem (in any version of PHP)...
You mean if fn bar has already been called in its own right before it gets called all over again from within fn foo?
Would be a pretty feeble function if it could only be used once
...what I really wanted was named constants, but I couldn't find them.
define('MY_CONSTANT','Hello World');
echo MY_CONSTANT; // "Hello World"
function foo() {
echo MY_CONSTANT; // "Hello World"
} If a constant is really what you require
They can be conditionally assigned any string
I need the navigation list to show what page and directory you're currently in. And there comes a point where doing it by hand just doesn't cut it, even if everything else is hand-coded
...
<body id="home">
...
<!--#include virtual="/menu.include"-->
...
<ul class="menu">
<li class="home"><a href="/">home</a></li>
<li class="services"><a href="/services/">services</a></li>
...
</ul>
...
<body id="services">
...
<!--#include virtual="/menu.include"-->
...
//style your menu without highlighting the current item
.menu li {
...
}
.menu li a {
...
}
//override on higher specificity the highlights
#home .home , #services .services , ... {
...
}
#home .home a, #services .services a, ... {
...
}
The easiest trick I ever used
Well, for a given definition of "easy" since it seems to require editing the stylesheet every time you add a page. Most of my navigation includes use the simple query