Reply to Re: global variables, Apache, PHP4.4.1Win32, WinXP

Your name:

Reply:


Posted by Al on 02/01/06 00:55

Jim Carlock wrote:
> How do I get a function to see a global variable without employing
> a parameter list?
>
> The source code:
>
> <html><head>
> <title>Simple Global Variable / Function Test</title>
> <?php
> $counter_file = "counter.txt"; // global variable
>
> //quickquote function
> function qq($sQuoteMe, $sQuoteChar) {
> return("$sQuoteChar$sQuoteMe$sQuoteChar");
> }
>
> function updatecount() {
> echo('<p>$counter_file = ' . "$counter_file</p>\n");
> }
> ?>
> </head>
> <body><?php
> echo('<p>$counter_file = ' . qq($counter_file, "&quot;") . "</p>\n");
> // echo('Hits: ' . qq("qq test", '&quot;'));
> echo('<p>Hits: ' . qq(updatecount(), "&quot;") . "</p>\n");
> ?></body></html>
>
> ... echos the following HTML to the browser ...
>
> <html><head>
> <title>Simple Global Variable / Function Test</title>
> </head>
> <body><p>$counter_file = &quot;counter.txt&quot;</p>
> <p>Hits: &quot;&quot;</p>
> </body></html>
>
> Thanks.
>
> Jim Carlock
> Post replies to the newsgroup.
>
> P.S. (For the people at php.net)...
> The Zend Optimizer link on this page results in a 404 page. Search for
> "Zend Optimizer" on the following page:
> http://us3.php.net/downloads.php


Just use global $counter_file; in your function.
See http://uk2.php.net/global#language.variables.scope.global for its
uage.

The global keyword will basically open up the variable for use by your
function. An alternative would be to say something like:

$counter_file = $GLOBALS["counter_file"];

but that only creates a local copy. However,

$counter_file = &$GLOBALS["counter_file"];

creates a read/writable reference like the gloabl keyword. That way you
can call it whatever you like.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация