define除了定义常量,还有什么作用?

WBOY
Release: 2016-06-23 13:31:55
Original
1516 people have browsed it




常量一旦定义不就不能改了吗?为什么给APP定义了这么多次?而常量的值似乎是路径,这似乎应该不是定义常量吧?
define("APP", "./admin");这句话的作用是什么?


回复讨论(解决方案)

define是定义常量的,常量可以是数字,也可以是字符串啊。。你echo APP;的时候,就是输出./admin这个了。和定义变量是类似的

 常量设定后,常量的值是无法更改的。define("APP", "./admin");,不是定义常量,作用是定义编译目录,也就是说app的作用域是在admin下。

常量是单个值的标识符(名称)。在脚本中无法改变该值。

define 就是定义常量,只要值符合语法规则,任何内容都可以
define("APP", "./admin"); 定义常量 APP 其值为 ./admin
使用 APP 就是使用 ./admin

至于在多个文件中有类似的定义,这是因为你的这些文件不是同时(在一个HTTP会话期间)执行的
执行 index.php 时 APP 为 ./home
执行 admin.php 时 APP 为 ./admin
执行 wap.php 时 APP 为 ./wap
这样就不必在程序里一一区分究竟是在那个目录下工作了

一个比较常见的用法是,限定文件必须从某个文件开始访问(如bootstrap文件),这种常见的模式是:
if(!defined("XXXX")) exit;

就是这么用,没有其他了。
defined 与 define是不同的方法。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!