469,293 Members | 1,319 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

Whats PHPs version of ASPs application scope variable

In ASP you can create a variable that is accessible by all scripts in
an application.

Is this possible in PHP? Storing a multi-dimensional array in memory
has much greater performance benefits than storing in a database or
session (which just gets saved to disk anyway). Id like to have my
commonly used application variables in memory.

Is this possible or is it one of the few flaws of PHP?

-Nick
Jul 17 '05 #1
7 15873

On 29-Dec-2003, nb********@hotmail.com (Nick) wrote:
In ASP you can create a variable that is accessible by all scripts in
an application.

Is this possible in PHP? Storing a multi-dimensional array in memory
has much greater performance benefits than storing in a database or
session (which just gets saved to disk anyway). Id like to have my
commonly used application variables in memory.

Is this possible or is it one of the few flaws of PHP?


http://www.php.net/manual/en/languag...bles.scope.php

scroll down to the global keyword
--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #2
Nick wrote:
Is this possible in PHP? Storing a multi-dimensional array in memory
has much greater performance benefits than storing in a database or
session (which just gets saved to disk anyway). Id like to have my
commonly used application variables in memory.

Is this possible or is it one of the few flaws of PHP?


There are a few ways to do this. One way is to use the 'global' keyword
in your functions. Another is to use the $GLOBALS array, but the one I
like most is to create a custom ini file and use parse_ini_file to read
the data. This makes for the easiest editing - especially by users.

Then if you set the auto_prepend_file setting via a .htaccess file, this
would be similar to using a globals.asp file (I think that's what it was
called anyway).

/includes/globals.php.ini
--------------------------
<?php exit; // in case it's called by a browser ?>
[section1]
var1 = val1
var2 = val2

[section2]
var1 = val2.1
var2 = val2.2

/includes/globals.php
---------------------
<?php
$CFG=parse_ini_file(dirname(__FILE__).'/globals.ini.php',TRUE);
?>

/test.php
---------
<?php
echo'<pre>';
print_r($CFG);
echo'</pre>';
?>

/.htaccess
----------
php_value auto_prepend_file "/home/user/www/includes/globals.php"

The above would then output this:
Array
(
[section1] => Array
(
[var1] => val1
[var2] => val2
)

[section2] => Array
(
[var1] => val2.1
[var2] => val2.2
)

)

If you don't set the second argument to parse_ini_file, then you'd get:
Array
(
[var1] => val2.1
[var2] => val2.2
)
--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #3
Nick <nb********@hotmail.com> wrote:
In ASP you can create a variable that is accessible by all scripts in
an application.

Is this possible in PHP? Storing a multi-dimensional array in memory
has much greater performance benefits than storing in a database or
session (which just gets saved to disk anyway).
You might use shared memory and semphores for this
http://nl3.php.net/manual/en/ref.sem.php
Id like to have my
commonly used application variables in memory.


If it's static data a simple include()/require() is better suited (might
be in the diskcache).

--

Daniel Tryba

Jul 17 '05 #4
PHP has no concept of an "application", unlike ASP.Net. PHP is a scripting
language, and from the view of PHP, the "application" is the current file
plus anything include()d or require()d.

Your best shot is probably either just writing out everything to a file like
vars.inc.php which you include on all pages (and re-writing that file as
necessary), or storing the information in some sort of database if that
turns out to be more practical.

Some people may call this a shortfall, others just see it as a design
philosophy. ASP.Net is designed around applications and tasks, PHP is
designed around scripts and procedures. (And no, I don't mean to start some
tech flame war, or anything like that. I'm simply saying that ASP.Net has a
concept of applications in the sense of multiple source files compiled into
a single application, supports OOP fully etc, whereas PHP is extremely well
suited towards procedural programming and scripting, but lacks the
facilities for full OOP and the concept of an "application" as opposed to
simply a collection of code.)

"Nick" <nb********@hotmail.com> wrote in message
news:ce**************************@posting.google.c om...
In ASP you can create a variable that is accessible by all scripts in
an application.

Is this possible in PHP? Storing a multi-dimensional array in memory
has much greater performance benefits than storing in a database or
session (which just gets saved to disk anyway). Id like to have my
commonly used application variables in memory.

Is this possible or is it one of the few flaws of PHP?

-Nick

Jul 17 '05 #5
Regarding this well-known quote, often attributed to Nick's famous "29 Dec
2003 11:42:50 -0800" speech:
In ASP you can create a variable that is accessible by all scripts in
an application.

Is this possible in PHP? Storing a multi-dimensional array in memory
has much greater performance benefits than storing in a database or
session (which just gets saved to disk anyway). Id like to have my
commonly used application variables in memory.

Is this possible or is it one of the few flaws of PHP?

-Nick


I'm not familiar with the ASP definition of an "Application". Does it
consist of only one HTML output (as in "a lot of includes, but only one
final output") or does the "Application" refer to multiple pages which
perform a common function?

If it's the former, AFAIK, any global variables, if you call them with
"global" in any functions, persist into and out of included files. If
you're talking the latter, then sessions are about the best way to go.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #6
Damn guys you dont know anything about ASP, only Agelmar know what is the Application concept in ASP, in PHP, the only comparaison
whould be Apache predefined variable, but you can't define your own as ASP do. The Application variable is all variables you
created for the whole server, this mean every ASP websites on this server can use these variables. There is not equivalence in PHP.
The only workaround whould be to use an include on all pages.
How sad :(

Savut

"Nick" <nb********@hotmail.com> wrote in message news:ce**************************@posting.google.c om...
In ASP you can create a variable that is accessible by all scripts in
an application.

Is this possible in PHP? Storing a multi-dimensional array in memory
has much greater performance benefits than storing in a database or
session (which just gets saved to disk anyway). Id like to have my
commonly used application variables in memory.

Is this possible or is it one of the few flaws of PHP?

-Nick

Jul 17 '05 #7
Savut wrote:
Damn guys you dont know anything about ASP
hmm... never did claim to know anything about it.
only Agelmar know what is the Application concept in ASP
Then perhaps the OP should have described it a bit more in detail to
give us a clue.
in PHP, the only comparaison
whould be Apache predefined variable, but you can't define your own as ASP do.
Actually, you can - in httpd.conf using similar to:
SetEnv TestString "Test String With Spaces"
SetEnv TestNumber 3157851357

This can go in the global for all hosted sites on the server to access,
or inside VirtualHost directives on a per-domain basis. Also, if you
want a number of domains to use them (but not all), you can place them
in a separate file and use the Include directive inside VirtualHosts to
get the values. You can also use that syntax in .htaccess files as well
to define your variables on a per directory basis.

You then access variables set with this method via the $_SERVER array.
The Application variable is all variables you
created for the whole server, this mean every ASP websites on this
server can use these variables. There is not equivalence in PHP.
The only workaround whould be to use an include on all pages.
How sad :(


Right, isn't that what I illustrated using parse_ini_file and
auto_prepend in my post? ;) Only difference is you don't have to add a
line of code to every file to include it.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by aa | last post: by
2 posts views Thread by Robert | last post: by
6 posts views Thread by shaun | last post: by
1 post views Thread by spolsky | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.