2013-11-28 31 views
1

所以我做在PHP中的以下内容:参数必须大于0 - 这怎么可能当值大于0

$cost = $this->reportDataStructure['ext_cost']/$this->reportDataStructure['quantity']; 
var_dump($cost); // Outputs: float(220) 
$this->reportDataStructure['cost'] = sprintf("%$.2f", $cost); 

而且我不断收到

Warning: sprintf(): Argument number must be greater than zero in C:\xampp\htdocs\rms\site\web\module\Report\controller\InventoryReport.controller.php on line 98 

其所属前往路线:

$this->reportDataStructure['cost'] = sprintf("%$.2f", $cost); 

但是,我们可以看到:

var_dump($cost); // Outputs: float(220) 

那么最新情况如何?

回答

6

$用于printf格式字符串以指定在其后面使用说明符打印哪个参数,例如,

sprintf('%2$f %1$d', $var1, $var2); 

意味着它将显示$var2为float然后$var1为整数。

您的格式字符串中有$,但在它之前没有数字。所以这个数字不大于0.

如果你用这个格式字符串解释你想要完成的事情,我可以用正确的方法更新答案。

+1

打我吧:) –

+0

我很幸运。直到今天早上我读到这个问题之前,我不知道这个'sprintf'功能:http://stackoverflow.com/questions/20261277/sprintf-using-the-same-values-multiple-times/20261358#20261358 – Barmar