定义站点范围的变量/常量 [英] Defining site-wide variables/constants

查看:60
本文介绍了定义站点范围的变量/常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,刚刚将一个站点从ASP转换为

PHP。但是有一件事我无法做到。


当网站使用ASP时,我有一个文件(称为variables.asp),

我定义了整个站点使用的各种变量。然后我可以通过简单地包含

该文件(使用#include),然后引用变量来访问任何其他文件中的所有这些文件。

我在PHP中无法做同样的事情。我一直在阅读

PHP手册并搜索Google,看起来好像用常量更好地完成了这些值(这些值不是必须的)更改为

脚本,只是打印到屏幕上),但我仍然无法获得

上班。


这是否可能,如果是这样,我做错了什么?


-

Mark Parnell
http://www.clarkecomputers.com.au

解决方案

Mark Parnell写道:

我是一个相对较新的PHP,刚刚将一个站点从ASP转换为PHP。但是有一件事我没办法做到。

当网站使用ASP时,我有一个文件(称为variables.asp),
我定义了各种变量整个网站使用的。然后我可以通过简单地包含那个文件(使用#include)来访问所有这些文件,然后引用变量。

我一直无法做到在PHP中同样的事情。我一直在阅读
PHP手册并搜索谷歌,似乎也许用常量更好地完成(这些值不需要通过脚本来改变) ,只是印在屏幕上),但我仍然无法让它工作。

这是否可能,如果是这样,我做错了什么?




使用PHP包含其中包含其他内容的文件

include(''filename'');


定义常量使用

define(''CONSTANT_NAME'',''CONSTANT VALUE'');


使用常量,例如回应:

echo CONSTANT_NAME;


设置变量使用的值:


< blockquote> variable_name ='''变量值'';


要使用变量,例如将其回显:

echo


variable_name;


请注意,在ASP / VBScript中,您可以使用所有变量你在任何地方宣布

想要的。你不必明确地将它们传递给子。因此,

以下会打印出bar到浏览器:


foo =''bar''

调用print_foo


sub print_foo

response.write foo

end sub


确切的PHP等价物是:


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

解决方案

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:


这篇关于定义站点范围的变量/常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆