By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

Defining site-wide variables/constants

P: n/a
I'm relatively new to PHP, and have just converted a site from ASP to
PHP. There is one thing I haven't managed to do, though.

When the site was using ASP, I had one file (called variables.asp),
where I defined various variables that are used throughout the site. I
could then access all of these in any other file by simply including
that file (using #include), then referencing the variables.

I have been unable to do the same thing in PHP. I have been reading the
PHP manual and searching Google, and it seems that perhaps it would be
better done with constants (the values don't ever need to be changed by
the script, just printed to the screen), but I am still unable to get it
to work.

Is this even possible, and if so, what am I doing wrong?

--
Mark Parnell
http://www.clarkecomputers.com.au
Jul 17 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
Mark Parnell wrote:
I'm relatively new to PHP, and have just converted a site from ASP to
PHP. There is one thing I haven't managed to do, though.

When the site was using ASP, I had one file (called variables.asp),
where I defined various variables that are used throughout the site. I
could then access all of these in any other file by simply including
that file (using #include), then referencing the variables.

I have been unable to do the same thing in PHP. I have been reading the
PHP manual and searching Google, and it seems that perhaps it would be
better done with constants (the values don't ever need to be changed by
the script, just printed to the screen), but I am still unable to get it
to work.

Is this even possible, and if so, what am I doing wrong?


To include a file with other stuff in it with PHP use
include('filename');

To define a constant use
define('CONSTANT_NAME', 'CONSTANT VALUE');

To use the constant, eg to echo it out:
echo CONSTANT_NAME;

To set a value for a variable use:
$variable_name = 'variable value';

To use the variable, eg to echo it out:
echo $variable_name;

Note that in ASP/VBScript you can use all the variables declare anywhere you
want to. You don't have to explicitly pass them into a sub. Thus, the
following would print out 'bar' to the browser:

foo = 'bar'
call print_foo

sub print_foo
response.write foo
end sub

The exact PHP equivalent of this is:

$foo = 'bar';
print_foo();

function print_foo() {
print $foo;
}

But this won't print anything out, as $foo is not defined in the print_foo()
function. Instead you'd need to do this:

$foo = 'bar';
print_foo($foo);

function print_foo($foo) {
print $foo;
}

or this using global (not recommended):

$foo = 'bar';
print_foo();

function print_foo() {
global $foo;
print $foo;
}

I hope this helps!

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2

P: n/a
On Wed, 16 Jun 2004 16:04:22 +1200, Chris Hope
<ch***@electrictoolbox.com> declared in comp.lang.php:
Note that in ASP/VBScript you can use all the variables declare anywhere you
want to. You don't have to explicitly pass them into a sub.
Up to this point it's OK - I know how to include files, declare
variables, etc. But this is where I run into problems. In ASP I can
define variables in one file, include that file in another, then just
print the variables. But in PHP...
$foo = 'bar';
print_foo($foo);

function print_foo($foo) {
print $foo;
}


I have to define a function for every one of my variables? That seems
like overkill (we're talking about ~50 variables here). I'm sure there's
a good reason for it, but still... Would that still be the case if I
used constants instead?

But even that doesn't seem to work, because the variable (and the
function) is defined in one file, and I am trying to use it in another.

Maybe I am not being clear enough in what I am trying to do. Let me
explain:

I have a file called variables.php, which contains:

$variable1="foo";
$variable2="bar";

I have another file called foobar.php, which contains:

include("variables.php");
echo($variable1);
echo($variable2);

And it doesn't return anything.

After reading your post, I tried the following:

variables.php:

$variable1="foo";
function variable1($variable1) {
print $variable1;
}

$variable2="bar";
function variable2($variable2) {
print $variable2;
}

foobar.php:

include("variables.php");
variable1($variable1);
variable2($variable2);

And it tells me that those functions are undefined.

--
Mark Parnell
http://www.clarkecomputers.com.au
Jul 17 '05 #3

P: n/a
Mark Parnell wrote:
I have to define a function for every one of my variables? That seems
like overkill (we're talking about ~50 variables here). I'm sure there's
a good reason for it, but still... Would that still be the case if I
used constants instead?


Ah no, you misunderstood or I didn't explain myself clearly enough. You
don't need the functions to use the variables; I was just trying to explain
that you can't reference a variable from outside a function unless it is
specifically passed in or declared global within the function.

Your example code looks fine to me and worked when I tried it just now. I
suspect your include file isn't actually being included, or the files
aren't being parsed as PHP. I'm not quite sure how to help you further.
Maybe someone else?

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #4

P: n/a
we*******@clarkecomputers.com.au says...
When the site was using ASP, I had one file (called variables.asp),
where I defined various variables that are used throughout the site. I
could then access all of these in any other file by simply including
that file (using #include), then referencing the variables.

I have been unable to do the same thing in PHP. I have been reading the
PHP manual and searching Google, and it seems that perhaps it would be
better done with constants (the values don't ever need to be changed by
the script, just printed to the screen), but I am still unable to get it
to work.

Is this even possible, Yes. and if so, what am I doing wrong?

Dunno, try the sample below.

<?php
// this is the contents of variables.php
define ('VARIABLE_ONE', 'Actually, it is a constant!');
define ("VARIABLE_TWO", "ABC123");
?>

<?php
// this is the contents of test.php
include 'variables.php';
echo 'The value of the first is = '.VARIABLE_ONE.'<br>';
$transitory_variable=VARIABLE_TWO;
echo 'The value of the second is = '.$transitory_variable.'<br>';
echo 'Happy coding!';
?>

Geoff M
Jul 17 '05 #5

P: n/a
> > I have been unable to do the same thing in PHP. I have been reading the
PHP manual and searching Google, and it seems that perhaps it would be
better done with constants (the values don't ever need to be changed by
the script, just printed to the screen), but I am still unable to get it
to work.


Further test script - refers to posting by Chris Hope:

<?php
// this is the contents of test_functions.php

$message='Does this work? - ';

include 'variables.php';
$var2=VARIABLE_TWO;

function test1 ($message) {
return $message.VARIABLE_TWO.'<br>';
}

function test2 ($message) {
return $message.$var2.'<br>';
}

function test3 ($message) {
include 'variables.php';
$var2=VARIABLE_TWO;
return $message.$var2.'<br>';
}

echo test1($message);
echo test2($message);
echo test3($message);

echo 'Happy coding!';
?>

Geoff M
Jul 17 '05 #6

P: n/a
On Wed, 16 Jun 2004 17:40:26 +1200, Chris Hope
<ch***@electrictoolbox.com> declared in comp.lang.php:
Your example code looks fine to me and worked when I tried it just now. I
suspect your include file isn't actually being included, or the files
aren't being parsed as PHP. I'm not quite sure how to help you further.
Maybe someone else?


Well, I know the file is being included, and parsed as PHP, because if I
add the line echo("foo"); to the variables file, it does print that on
screen. Will try Geoff's code, though it seems to be essentially the
same.

--
Mark Parnell
http://www.clarkecomputers.com.au
Jul 17 '05 #7

P: n/a
On Wed, 16 Jun 2004 05:51:34 GMT, gmuldoon <gm*************@scu.edu.au>
declared in comp.lang.php:
Dunno, try the sample below.


Thanks, Geoff - this worked perfectly. No idea what was going on before.
All's well with the world again. :-)

Now, if I can just work out how to get mail() to send HTML email...but
that's another thread.

--
Mark Parnell
http://www.clarkecomputers.com.au
Jul 17 '05 #8

P: n/a
Mark Parnell wrote:
Now, if I can just work out how to get mail() to send HTML email...but
that's another thread.


Rather than try to figure out how to do it yourself (and it requires a
fairly thorough understanding of how an email is constructed), take a look
at the PEAR mime mail stuff at

http://pear.php.net/package/Mail
http://pear.php.net/package/Mail_Mime

I've been meaning to write an article with example usage of this class for a
while now. Maybe some day I'll get time to :)

There are some other classes around for doing this as well but I haven't
used any of those ones.

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #9

P: n/a
On Wed, 16 Jun 2004 18:35:48 +1200, Chris Hope
<ch***@electrictoolbox.com> declared in comp.lang.php:
Rather than try to figure out how to do it yourself (and it requires a
fairly thorough understanding of how an email is constructed), take a look
at the PEAR mime mail stuff at


Will do, thanks. :-)

--
Mark Parnell
http://www.clarkecomputers.com.au
Jul 17 '05 #10

P: n/a
I noticed that Message-ID: <1b******************************@40tude.net>
from Mark Parnell contained the following:
I'm relatively new to PHP, and have just converted a site from ASP to
PHP.


I'd be interested in reasons for people changing from asp to php

A system administrator I may have to work with is not very complimentary
towards PHP and thinks that asp.NET is the one true way.

Is this just snobbishness of does what he say have some merit?

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #11

P: n/a
I noticed that Message-ID: <12*****************************@40tude.net>
from Mark Parnell contained the following:
I have a file called variables.php, which contains:

$variable1="foo";
$variable2="bar";

I have another file called foobar.php, which contains:

include("variables.php");
echo($variable1);
echo($variable2);

And it doesn't return anything.


I've had this. The include file needs to be enclosed in <?php ?> tags

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #12

P: n/a
On Wed, 16 Jun 2004 08:10:24 +0100, Geoff Berrow <bl******@ckdog.co.uk>
declared in comp.lang.php:
I'd be interested in reasons for people changing from asp to php


In our case the site was hosted on a Linux server with Sun Chili!Soft
ASP, which made some things very difficult, so we switched to PHP.

--
Mark Parnell
http://www.clarkecomputers.com.au
Jul 17 '05 #13

P: n/a
Geoff Berrow wrote:
I'd be interested in reasons for people changing from asp to php

A system administrator I may have to work with is not very complimentary
towards PHP and thinks that asp.NET is the one true way.


ASP/VBscript is not the same as ASP.NET. I've used PHP, ASP/VBScript
and .NET (both C# and Visual Basic.NET) to create web applications and
email marketing systems.

VBScript is really limited and there are so few useful built-in functions.
You need to buy components if you want to do anything even slightly tricky
(such as uploading a file). Between VBScript and PHP there's no comparison;
PHP wins all the way.

When comparing C# and VB.NET with PHP it's a slightly different story. There
is a huge amount of stuff available in the .NET framework for doing all
sorts of useful things. In a lot of respect it comes down to personal bias
(which is often the reason for choosing one language over another) and
arguments for and against are often equally valid.

I personally would choose PHP over .NET any day of the week. However I would
also choose a non-Microsoft based platform over an MS platform any day of
the week so my perception is somewhat skewed in that respect, and for that
reason I'm more inclined to use something like PHP or Perl or Python which
can be used on multiple platforms than something like .NET which is
currently limited to Windows, although the Mono project sounds like it is
making pretty good progress on creating an open-sourced implementation of
C#/.NET.

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #14

P: n/a
Ceg
"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
news:oa********************************@4ax.com...
I noticed that Message-ID: <1b******************************@40tude.net>
from Mark Parnell contained the following:
I'm relatively new to PHP, and have just converted a site from ASP to
PHP.


I'd be interested in reasons for people changing from asp to php


I can't seem to find a server that runs ASP very well on WinXP Home. I'm
currently running Apache::ASP and the server from Pablo van der Meer. If
anyone knows of a server than handles ASP "out of the box" on WinXP Home I'd
sure appreciate hearing about it.


Jul 17 '05 #15

P: n/a
On Wed, 16 Jun 2004 08:52:20 -0500, "Ceg" <me@privacy.net> wrote:
"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
news:oa********************************@4ax.com.. .
I noticed that Message-ID: <1b******************************@40tude.net>
from Mark Parnell contained the following:
>I'm relatively new to PHP, and have just converted a site from ASP to
>PHP.


I'd be interested in reasons for people changing from asp to php


I can't seem to find a server that runs ASP very well on WinXP Home. I'm
currently running Apache::ASP and the server from Pablo van der Meer. If
anyone knows of a server than handles ASP "out of the box" on WinXP Home I'd
sure appreciate hearing about it.


For some bonkers reason, Microsoft precludes you installing either of
their web server products - IIS and Personal Web Server - on WinXP
Home. So you either use another MS OS and install IIS/PWS, or use
another web server with an ASP module like you're doing just now.

Or use PHP!

--
David ( @priz.co.uk )
Jul 17 '05 #16

P: n/a
In article <sgYzc.118855$Yr.29990@okepread04>, me@privacy.net says...
"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
news:oa********************************@4ax.com...
I noticed that Message-ID: <1b******************************@40tude.net>
from Mark Parnell contained the following:
I'm relatively new to PHP, and have just converted a site from ASP to
PHP.


I'd be interested in reasons for people changing from asp to php


I can't seem to find a server that runs ASP very well on WinXP Home. I'm
currently running Apache::ASP and the server from Pablo van der Meer. If
anyone knows of a server than handles ASP "out of the box" on WinXP Home I'd
sure appreciate hearing about it.


If you upgrade to a platform that was designed for development you would
be a lot better off. Windows XP Professional, while not a server OS, has
IIS (ASP) available and it works fine for up to 10 connections. XP Home
is just a waste of time for developers.

There are several ways to get MS Software that are 100% legit, you can
purchase it at a retail store, purchase an OEM copy when you purchase
hardware, or you can purchase the "Action Pack" from MS when you
register as a MS Partner - the Action Pack is for developers/companies
that intend on developing/pushing MS products to customers, it comes
with 10 licenses for XP Prof, Office XP/2003, Small Business Server,
Exchange Server, etc... Even has 2003 Server in it. All for legit use in
a SOHO - it's about $299/year to maintain the license.

So, for the cost of one XP Prof retail purchase you can get the action
pack directly from MS and have multiple servers, 10 installs of XP prof,
and 10 installs of the Office/Visio/Publisher products for your company.
Even a 1 person company qualifies!
--
--
sp*********@rrohio.com
(Remove 999 to reply to me)
Jul 17 '05 #17

P: n/a
On Wed, 16 Jun 2004 14:29:33 GMT, Leythos <vo**@nowhere.com> wrote:
In article <sgYzc.118855$Yr.29990@okepread04>, me@privacy.net says...
"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
news:oa********************************@4ax.com...
> I noticed that Message-ID: <1b******************************@40tude.net>
> from Mark Parnell contained the following:
>
> >I'm relatively new to PHP, and have just converted a site from ASP to
> >PHP.
>
> I'd be interested in reasons for people changing from asp to php


I can't seem to find a server that runs ASP very well on WinXP Home. I'm
currently running Apache::ASP and the server from Pablo van der Meer. If
anyone knows of a server than handles ASP "out of the box" on WinXP Home I'd
sure appreciate hearing about it.


If you upgrade to a platform that was designed for development you would
be a lot better off. Windows XP Professional, while not a server OS, has
IIS (ASP) available and it works fine for up to 10 connections. XP Home
is just a waste of time for developers.

There are several ways to get MS Software that are 100% legit, you can
purchase it at a retail store, purchase an OEM copy when you purchase
hardware, or you can purchase the "Action Pack" from MS when you
register as a MS Partner - the Action Pack is for developers/companies
that intend on developing/pushing MS products to customers, it comes
with 10 licenses for XP Prof, Office XP/2003, Small Business Server,
Exchange Server, etc... Even has 2003 Server in it. All for legit use in
a SOHO - it's about $299/year to maintain the license.


I can thoroughly recommend the Action Pack.

I work for a small software company and we find the value of the
Action Pack unbeatable.

IIRC we didn't have to register as a Partner. AFAIK in the UK, any
registered company can apply for it.

--
David ( @priz.co.uk )
Jul 17 '05 #18

P: n/a
Geoff Berrow wrote:
I noticed that Message-ID: <1b******************************@40tude.net>
from Mark Parnell contained the following:
I'm relatively new to PHP, and have just converted a site from ASP to
PHP.


I'd be interested in reasons for people changing from asp to php

A system administrator I may have to work with is not very complimentary
towards PHP and thinks that asp.NET is the one true way.

Is this just snobbishness of does what he say have some merit?


I spent the past 2.5 years working in ASP.NET (VB and C#) on a long-term
consulting project for CalHFA (a CA state agency). It is an excellent
product and I highly recommend it..... for inTRAnet corporate web-based apps.
If you have the money and you are already an MS shop and you already have MS
SQL-Server and you are comfortable being locked into a single solution
provider, than you can't go wrong with this platform. I highly recommend it.

However, before our company decided on a platform for our OWN Internet based
web-application, JAYA123 (http://www.jaya123.com) I did an exhaustive study.
We knew we would spend 18 months programming / testing it and would spend
over $100K in labor and we were betting the company... so we had to be right.
Failure was not an option.

I looked at everything from A(sp) to Z(ope) and everything in between....
Jayton, Tomcat, Websphere, BEA... the whole works. All had their pluses and
minuses. None were "bad."

Our number one concern was security... and I felt that I would not sleep at
night running on an IIS server open to the net. Fine for an inTRAnet, but I
have doubts about world-wide access to it. Same with some of the other
servers.

At the end of the day... after I had looked and prototyped everything... we
decided to "keep it simple stupid" and opted for a tried, true, safe, and
sound LAMP platform.... Linux, Apache, MySQL, PHP. Our application is not as
visually "slick" as one might get from some of the components in ASP.Net, but
we are not locked in, our app runs on every browser except (Netscape 4.0...
and we don't know why!). Our goal was speed for a dial-up user... as well as
to keep away from Javascript that can choke some browsers on some machines.
So we wrote about 155,000 lines of PHP code and about 55 lines of Javascript
and that was that.

While ASP is "nice" because of all the objects (grids, dropdowns, etc.) you
are not "alone" when using PHP. We used a terrific database abstraction class
called ADODB as well as a wonderful PDF generator class called fpdf.org.
There are other classes as well to do most of what you might want to do...
however granted you will have to write a bit more code to use them, then in
an ASP.Net environment. Everything has a cost.

If you want to see just how complex an application (with masterful reports)
you can get with PHP, just go to the DEMO of our JAYA123 application and
check it out. (No personal info required to login).

I'm not knocking ASP.Net. I think MS got it "right." Once it can be run on a
very secure platform, it will be ready for "the wild." But it's not yet, in
my opinion... and I have the background and the credentials to make that
statement (although I won't go into them here.)
Al Canton, President
Adams-Blake Company, Inc.
***
JAYA123 - the new web-based total-office system for the
small biz. Order entry, billing, bookkeeping, etc. for $14.95
a month. Everyone says "It's cool as a moose!!"
See why at:http://www.jaya123.com ('ja-eye-ah' means
'victory' in Sanskrit.)
***

Jul 17 '05 #19

P: n/a
In article <10*************@news20.forteinc.com>, atakeoutcanton@adams-
blaketakeout.com says...
I'm not knocking ASP.Net. I think MS got it "right." Once it can be run on a
very secure platform, it will be ready for "the wild." But it's not yet, in
my opinion... and I have the background and the credentials to make that
statement (although I won't go into them here.)


Al, I have to disagree with you on the security of the platform of ASP /
ASP.Net. We've done many state agency projects used by thousands of
workers all days long all over the state, fully exposed to the internet.
We've done more than 100 sites for fortune 100 companies that process
everything from product information to product orders on ASP platforms,
now moving to .Net platforms.

Our own company designs on every platform, not just the MS platform, and
in every language - we use the right platform for the job or the one
that the customer demands that we use. In all of our years of working
with MS IIS based systems we've not had one customer site compromised in
any manner and not one web server has been brought down by an attack.

The same can be said for all of our BEA, CF, CFMX, Nix servers.

It's all in designing a secure platform for your servers, a secure
network, and knowing what to allow in/out through the firewall. Our
state clients are still running ASP.Net servers exposed to the public
and have not had any problems - they started with pre-RC1 .Net installs
and have never looked back.
--
--
sp*********@rrohio.com
(Remove 999 to reply to me)
Jul 17 '05 #20

P: n/a
Mark Parnell <we*******@clarkecomputers.com.au> wrote
in message news:<my****************************@40tude.net>.. .

Now, if I can just work out how to get mail() to send HTML email...


Easy enough... Get yourself a copy of phpMailer:

http://phpmailer.sf.net/

Cheers,
NC
Jul 17 '05 #21

P: n/a
On Wed, 16 Jun 2004 08:52:20 -0500, Ceg <me@privacy.net> declared in
comp.lang.php:
I can't seem to find a server that runs ASP very well on WinXP Home. I'm
currently running Apache::ASP and the server from Pablo van der Meer. If
anyone knows of a server than handles ASP "out of the box" on WinXP Home I'd
sure appreciate hearing about it.


It is apparently possible to get IIS running on XP Home (you need a
Win2000 CD, though).

http://www.ipwebdesign.net/kaelisSpa...l_iisOnXp.html
http://www.chriscodes.com/articles/id-3/

Disclaimer: I haven't tried it myself. :-)

--
Mark Parnell
http://www.clarkecomputers.com.au
Jul 17 '05 #22

P: n/a
Mark Parnell <we*******@clarkecomputers.com.au> wrote in message news:<1b******************************@40tude.net> ...
I'm relatively new to PHP, and have just converted a site from ASP to
PHP. There is one thing I haven't managed to do, though.

<snip>

FWIW, <http://asp2php.naken.cc/> is worth to try.

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #23

P: n/a
On 16 Jun 2004 23:30:45 -0700, "R. Rajesh Jeba Anbiah"
<ng**********@rediffmail.com> declared in comp.lang.php:
FWIW, <http://asp2php.naken.cc/> is worth to try.


That's actually what I used in the first place, thanks. Saved a lot of
time, but it still required a fair bit of work to get everything working
properly.

--
Mark Parnell
http://www.clarkecomputers.com.au
Jul 17 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.