php 如何通过php的require()或include()函数传递变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5128137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:53:22  来源:igfitidea点击:

how to pass a variable through the require() or include() function of php?

php

提问by sof_user

when I use this:

当我使用这个时:

require("diggstyle_code.php?page=$page_no");

the warning is :failed to open stream: No error in C:\xampp\htdocs\4ajax\gallery_core.php on line 198

警告是 :failed to open stream: No error in C:\xampp\htdocs\4ajax\gallery_core.php on line 198

and the error is:

错误是:

Failed opening required 'diggstyle_code.php?page=1' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocsajax\gallery_core.php on line 198

value of the variable $page_nois collected beforehand.

变量的值$page_no是预先收集的。

But if I omit the '?page=$page_no part'from the argument of the require function, then no error or warning is shown.

但是,如果我'?page=$page_no part'从 require 函数的参数中省略了,则不会显示错误或警告。

I need to pass the variable when I use the require() function.

我需要在使用 require() 函数时传递变量。

回答by Pascal MARTIN

require()and include()will open the file corresponding to the path/name they receive.

require()include()会打开相应的他们收到的路径/文件名的文件。

Which means that, with your code, you would have to have a file called diggstyle_code.php?page=1on your disk. That's obviously not the case, so it fails.

这意味着,使用您的代码,您必须diggstyle_code.php?page=1在磁盘上调用一个文件。显然不是这样,所以它失败了。

Quoting the Variable scopepage of the PHP Manual:

引用PHP 手册的Variable scope页面:

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.

变量的作用域是定义它的上下文。大多数情况下,所有 PHP 变量都只有一个作用域。这个单一的范围也涵盖了包含和必需的文件。

In your case, you don't need to pass the variable. If you have a variable in your current script, it will also exist in the script you include, outside of functions, which have their own scope.

在您的情况下,您不需要传递变量。如果您当前的脚本中有一个变量,它也将存在于您包含的脚本中,在函数之外,有自己的作用域

In your main script, you should have:

在你的主脚本中,你应该有:

$page_no = 10;
require 'diggstyle_code.php';

And in diggstyle_code.php:

并在diggstyle_code.php

echo $page_no;
// Or work with $page_no the way you have to


Remember that including/requiring a file is exactly the same as copy-pasting its content at the line it's required.

请记住,包含/要求文件与将其内容复制粘贴到所需行完全相同。

回答by knittl

this should work, but it's quite a dirty hack:

这应该有效,但这是一个相当肮脏的黑客:

$_GET['page'] = $page_no;
require('diggstyle_code.php');

you probably want to refactor your code to use functions and/or objects and call them from your files instead of including them (spaghetti code alert)

您可能想要重构代码以使用函数和/或对象并从文件中调用它们而不是包含它们(意大利面条代码警报)

回答by Blagovest Buyukliev

requiredoesn't pull the file from the web server - it should refer to a file on the filesystem instead.

require不会从 Web 服务器中提取文件 - 它应该引用文件系统上的文件。

Calling includeor requirejust tells PHP to paste the contents of the given file in your code at this place, nothing more than that.

调用includerequire只是告诉 PHP 将给定文件的内容粘贴到您的代码中,仅此而已。

回答by Marty

Though this question is old there's another option that I use which is missing from this thread. You can return a function from the required file which accepts the arguments you want to pass along:

虽然这个问题很旧,但我使用的另一个选项是这个线程中缺少的。您可以从接受您想要传递的参数的所需文件中返回一个函数:

return function(array $something) {
    print_r($something);
}

And call it with the arguments when you requireit:

并在您调用它时使用参数调用require它:

require('file.php')(['some', 'data']);

// or:

$context = require('file.php');
$context(['some', 'data']);

回答by Tormy Van Cool

if I've correctly understood, what you need is to call the file diggstyle_code.phppassing an argument, so that no one can call that file and make it work, rather than your main file. Am I right?

如果我理解正确的话,您需要的是调用diggstyle_code.php传递参数的文件,以便没有人可以调用该文件并使其工作,而不是您的主文件。我对吗?

Thus supposing that your "main.php" has the lines

因此假设你的“main.php”有几行

require("diggstyle_code.php?page=$page_no");

it means that: if anyone calls "main.php" gets diggstyle_code.phprunning. But if anybody in any manner calls directly diggstyle_code.phphe/she shoudl get nothing.

这意味着:如果有人调用“main.php”就会diggstyle_code.php运行。但如果有人以任何方式直接打电话,diggstyle_code.php他/她应该一无所获。

If I am right on my understanding, a way to achieve this, is to include into the main file a variable or a constant, that will be scoped by diggstyle_code.php

如果我的理解是正确的,实现此目的的一种方法是在主文件中包含一个变量或常量,其范围为 diggstyle_code.php

Thus for instance: 'main.php'

例如:'main.php'

<?php
define("_VERIFICATION_", "y");
require("diggstyle_code.php");
?>

and now diggstyle_code.php

现在 diggstyle_code.php

<?php
if ( _VERIFICATION_ == "y" ) {
//Here the code should be executed
} else {
// Something else
}
?>

回答by stephane k.

If your variable is global, there's no need to "pass"it, it is there already: PHP variable scope.

如果您的变量是全局变量,则无需“传递”它,它已经存在: PHP 变量范围

The answer then is, don't do anything, if $page_no exists in the file in which you call require(), it will be available in the included file.

答案是,不要做任何事情,如果 $page_no 存在于您调用 require() 的文件中,它将在包含的文件中可用。

回答by linepogl

require()does not make an HTTP call. All it does is open the file from disk and include the code in the position of the call. So simple local variables are enough.

require()不进行 HTTP 调用。它所做的只是从磁盘打开文件并将代码包含在调用位置。所以简单的局部变量就足够了。

回答by krtek

require, require_once, include and include_once try to include files from the filesystem in the current file.

require、require_once、include 和 include_once 尝试将文件系统中的文件包含在当前文件中。

Since there's no files named diggstyle_code.php?page=1, it's totally logical that PHP can't find it.

由于没有名为 diggstyle_code.php?page=1 的文件,PHP 找不到它是完全合乎逻辑的。

You can't pass values that way, however, any variable declared in the current file will be accessible in the included files.

您不能以这种方式传递值,但是,当前文件中声明的任何变量都可以在包含的文件中访问。

回答by user3420355

I had this problem and I noticed if you use http:// in your url then it doesn't work

我遇到了这个问题,我注意到如果您在 url 中使用 http:// 则它不起作用