简体   繁体   English

php 定义常量 - scope 的用途?

[英]php define constant - scope of use?

Not wanting this question to be too long, I will skip to an example:不想这个问题太长,我会跳到一个例子:
If I have 2 files: paper.php and rock.php, and they contain the following:如果我有 2 个文件:paper.php 和 rock.php,它们包含以下内容:

paper.php:纸.php:

include('rock.php');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

and rock.php:rock.php:

define ("DB_HOST", "localhost");
define ("DB_USER", "foo");
define ("DB_PASS","bar");
define ("DB_NAME","fooDBar");

Eventually, will the user viewing my paper.php file be connected to the DB or not?最终,查看我的论文的用户是否会连接到数据库。php 文件?

Not wanting the answer to be too long:不希望答案太长:

Yes.是的。

Yes, you define all the appropriate variables in rock.php and are including rock.php , then they will be defined for the whole program execution, including where you do a mysql_connect() .是的,您在rock.php中定义了所有适当的变量,并且包括rock.php ,那么它们将为整个程序执行定义,包括您在哪里执行mysql_connect()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM