creating new files depending on what argument it passes in cli

Hello,

I can’t seem to make it create two files.

Script file: test.php

[php]<?php

define("SRV_RT",realpath(dirname(__FILE__).'/..'));	

	if ($argv =0 ){
	$data = SRV_RT."/create_file1.txt";
	}
	else{
	$data = SRV_RT."/create_file2.txt";
	}

$handle = fopen($data, "w+") or die("can't open file");
fwrite($handle, 'WORK DAMMIT!');
fclose($handle);

?>[/php]

What it suppose to do is if I just run the script it suppose to make create_file1.txt and if I pass any arguments it suppose to create create_file2.txt. The problem is when I try to run $argv[0] and $argv[1] it only makes create_file2.txt

cli commends:
php test.php --> creates create_file2.txt
php test.php arg1 --> creates create_file2.txt

What to do?

Thats because $argv already has a value, and I think you want to use $argc not $argv
$argc returns the command argument count
$argv returns the arguments themselves

always check your variables

<?php var_dump($argc,$argv); ?>

Sponsor our Newsletter | Privacy Policy | Terms of Service