env vars in included _functions_?
| From: | (Sean Straw / PSE) | Date: | Thu, 30 Nov 2000 12:15:27 +0000 |
| Subject: | env vars in included _functions_? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-28024@lists.php.net to get a copy of this message | ||
Server: Apache/1.3.14 Ben-SSL/1.42 (Unix) PHP/4.0.3pl1
I'm having a problem with env vars in an included function. For a simple example:
-- phptest1.php
<?PHP //phptest1.php
echo "1: $PHP_SELF<P>\n";
?>
-- phptest2.php
<?PHP //phptest2.php
function phptest2 ()
{
echo "2: $PHP_SELF<P>\n";
}
?>
-- phptest3.php
<?PHP //phptest3.php
$iare = getenv("PHP_SELF");
echo "3: $iare<P>\n";
?>
-- phptest4.php
<?PHP //phptest4.php
function phptest4 ()
{
$iare = getenv("PHP_SELF");
echo "4: $iare<P>\n";
}
?>
-- phptest5.php
<?PHP //phptest5.php
echo "5: $HTTP_SERVER_VARS[PHP_SELF]<P>\n";
}
?>
-- phptest6.php
<?PHP //phptest6.php
function phptest6 ()
{
echo "6: $HTTP_SERVER_VARS[PHP_SELF]<P>\n";
}
?>
-- phptest.php
<?PHP include ("phptest1.php"); include ("phptest2.php"); phptest2 (); include ("phptest3.php"); include ("phptest4.php"); phptest4 (); include ("phptest5.php"); include ("phptest6.php"); phptest6 (); ?>
--
When one loads up the phptest.php script (via a browser, not the commandline), this results in:
--
1: /test/phptest.php
2:
3:
4:
5: /test/phptest.php
6:
--
Now, certainly, PHP_SELF isn't a real environmet variable. So both of the getenv() functions can be expected to fail. But why does do both of the other included_as_function (phptest2 and phptest6) implementations fail? It seems only _INLINE CODE_, not included functions, can make use of env variables - if the functions are part of the main module, no problem, but if they're included, kiss variables goodbye.
Is this a bug, or did I miss something basic?
FTR, I fell on this annoyance when scribbling up a really basic link-unless-this-is us function, for wrapping some page elements so that they're not a link if they'd be linking to this same page (useful for included headers and footers). Since I'd just as soon use this as a function, included in a bigger library, rather than including it all over the place as embedded code, It would be nice it the environment variables worked.
<?PHP // condlink.php
// link the provided material to a given url as long as the current
// document IS NOT equal to the given document name.
//
// content is the material to show - either linked, or by itself.
// page is the server-relative document path you're comparing against
// link is the link to use in the created HREF, may be blank if you
// want to use page (which most often, you probably will)
//
// example:
// condlink( <IMG SRC="banner.gif">, '/index.php', '/index.php' );
// which is the same as:
// condlink( <IMG SRC="banner.gif">, '/index.php' );
//
// SBS 29 NOV 2000 18:42:02 - created
function condlink ( $content, $page, $link=NULL )
{
if ($link == "")
{
// assume same as page
$link = $page;
}
// if the CURRENT page == $page, then DON'T link,
// otherwise, link to $link
if ( $PHP_SELF == $page )
{
echo $content;
}
else
{
echo "<A HREF=\"$link\">$content</A>";
}
}
?>
---
Please DO NOT carbon me on list replies. I'll get my copy from the list.
Sean B. Straw / Professional Software Engineering
Post Box 2395 / San Rafael, CA 94912-2395