简体   繁体   English

以下 php 代码的含义是什么?

[英]meaning of the following php code?

Guys I have been using the following code quite often but haven't properly understood the exact semantics of the functions.伙计们,我经常使用以下代码,但还没有正确理解函数的确切语义。 Especially for the last two str_replace functions.特别是对于最后两个 str_replace 函数。 Can anybody explain me whats exactly happening here(in a little detail)?谁能解释一下这里到底发生了什么(稍微详细一点)?

$this_file = str_replace('\\', '/', __FILE__);
$doc_root = $_SERVER['DOCUMENT_ROOT'];
$web_root = str_replace(array($doc_root, 'application/config.php'), '', $doc_root);
$srv_root = str_replace('application/config.php', '', $doc_root);
  1. Replace the backslashes \ with forward slashes / (the variable is not used after that).将反斜杠\替换为正斜杠/ (之后不使用该变量)。
  2. Get the document root from the server super-global (typically will be the Apache config DocumentRoot directive).从服务器超级全局获取文档根目录(通常是 Apache 配置 DocumentRoot 指令)。
  3. Replace any occurence of the document root or application/config.php from the document root string and store it in $web_root (seems like useless code to me).从文档根字符串中替换任何出现的文档根或application/config.php并将其存储在$web_root中(对我来说似乎是无用的代码)。
  4. Replace any occurence of application/config.php from the document root string and store it in $srv_root (again, seems like useless code, because a document root should be a directory, not a file).从文档根字符串中替换任何出现的application/config.php并将其存储在$srv_root中(同样,这似乎是无用的代码,因为文档根应该是一个目录,而不是一个文件)。

Conclusion: It seems like this code could be translated into the following, and that none of these "calculations" are actually necessary:结论:似乎这段代码可以翻译成以下内容,并且这些“计算”实际上都不是必需的:

$doc_root = $_SERVER['DOCUMENT_ROOT'];
$web_root = '';
$srv_root = $doc_root;

Or even shorter:甚至更短:

$doc_root = $srv_root = $_SERVER['DOCUMENT_ROOT'];
$web_root = '';

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

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