将字符串作为2个或多个参数传递到函数中


pass string into function as 2 or more arguments

你好,我只是测试一些东西来理解的概念

我正试图做到这一点:

$args = "'file.txt', 'w'";
fopen($args);

但fopen认为这是的一个论点

我错过了什么?

在PHP 5.6中,您可以使用参数拆包,在我看来,这与您尝试的最接近:

$args = ['file.txt', 'w'];
fopen(...$args);

如果你打开一个文件,那么为什么不使用file_put_contents呢?它可能更容易使用。

http://php.net/manual/en/function.file-put-contents.php

您要做的是创建一个"file.txt','w'"字符串,并将其作为一个参数提交。

fopen():的方法签名

fopen(string$filename,string$mode[,bool$use_include_path=false[,resource$context]])

你想做的是:

$file = 'file.txt';
$mode = 'w';
fopen($file, $mode);

不能像这样传递参数。这样做的方法如下:

<?
$args = array("file.txt","w");
$fopen($args[0],$args[1]);
?>

您应该在fopen()中放入2个参数

<?
$args = array("file"   => "file.txt",
              "option" => "w");
fopen($args["file"],$args["option"]);
?>