k961
k961

Reputation: 587

pass string into function as 2 or more arguments

Hello im just testing things to understand concepts

im trying to do this :

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

but fopen sees it as one arguments

what am i missing ?

Upvotes: 1

Views: 80

Answers (5)

Kai Mattern
Kai Mattern

Reputation: 3085

What you do is creating one string that is "'file.txt', 'w'" and submit that as ONE argument.

See the method signature of fopen():

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

What you want to do is:

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

Upvotes: 1

x-ray
x-ray

Reputation: 3329

In PHP 5.6 you can use argument unpacking which in my opinion is closest to what you tried:

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

Upvotes: 1

Kevin medou
Kevin medou

Reputation: 49

you should put 2 parameters in fopen()

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

fopen($args["file"],$args["option"]);
?>

Upvotes: 0

Adam T
Adam T

Reputation: 675

If you're opening a file to so some writing to it, why not use file_put_contents - it might be easier to use.

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

Upvotes: 1

TheAlexLichter
TheAlexLichter

Reputation: 7289

You can't pass the arguments like this. A way to do this would be the following:

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

Upvotes: 0

Related Questions