我想通过命令行传递$ n的变量值,例如$ php fibo.php 6
<?php
class fibo
{
function fibo1($n)
{
$first = 0;
$second = 1;
echo "Fibonacci Series \n";
echo $first." ".$second." ";
$n = $argv[1];
for ($i=2; $i < $n ; $i++)
{
$third = $first + $second;
echo $third." ";
$first = $second;
$second = $third;
}
}
}
fibo::fibo1($n);
?>
我得到了这个输出
$ php fibo.php 6
斐波那契系列 0 1
但是在删除类和函数之后它才有效。 e.g。
$ php fibo.php 10
Fibonacci Series
0 1 1 2 3 5 8 13 21 34
答案 0 :(得分:0)
尝试此代码
class fibo
{
function fibo1($n)
{
$first = 0;
$second = 1;
echo "Fibonacci Series \n";
echo $first." ".$second." ";
for ($i=2; $i < $n ; $i++)
{
$third = $first + $second;
echo $third." ";
$first = $second;
$second = $third;
}
}
}
$n=getopt(null, ["n:"]);
fibo::fibo1($n['n']);
运行此命令 $ php fibo.php --n = 6