473,409 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

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 16047

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: aa | last post by:
I am migrating to PHP from ASP where there are the Application Scope variables which are accessible from any page on a website and which are used, in particular, for hit counters. Is there a similar...
2
by: Robert | last post by:
I have an application migrated from 1.1 to 2.0 via VS2005. Under 1.1 I could do the following to return the version information (as found in the assemblyinfo.vb file) GetVersion =...
6
by: shaun | last post by:
If I put (define) const variables at the top of a .cpp file but do not declare them in the .h file, are they only visible within that .cpp file? e.g. const int a={1,2,3,4}; in a cpp file...
5
by: Rich | last post by:
The following code produced a singleton object with application scope when it should have had page scope: public class Singleton { private static Singleton uniqueInstance = null; private...
1
by: spolsky | last post by:
i use an application scope recordset object for holding the list of online users. i am not sure that if i have to handle concurrency issues like one user is looping through the recordset for...
0
by: José Joye | last post by:
Hello, I'm trying to use the VS2005 Settings file so as to get strongly typed settings. I want the following: - Have all my application setting (with application scope) defined centrally in my...
0
by: ellon | last post by:
I found a good way to do that: <?php function application_start (){ global $_APP; global $shm_id; $shm_id = shmop_open(0xff3, "c", 0644, 10000); $shm_size = shmop_size($shm_id); ...
0
by: Umbrae | last post by:
Hey guys, Just wanted to ask your opinion - I just wrote up and released as Open Source a class called AppCache. AppCache is an easy to use class implementation of an Application scope in PHP,...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.