每次向变量添加2

时间:2016-01-11 14:47:23

标签: php

我需要变量$ Dno,每次在循环中打印时添加两个。

以下是我的代码。

$Dno = '18' + '2';



        while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
        {   

            //Description Binder from this Table in DB
            while($ffdata=$qqq->fetch(PDO::FETCH_ASSOC))
            {
                    $dta =  $ffdata['code'];
                    $sq = "SELECT * FROM hcpc WHERE code = :dta";
                    $qr=$con->prepare($sq);
                    $qr->bindvalue(":dta", $dta);
                    $qr->execute();
                    while($hcdata=$qr->fetch(PDO::FETCH_ASSOC))
                    {
                        $worksheet
                            ->setCellValue('C' . $Dno++, $hcdata['description']);
                    }
            }   

        }

正如你所看到的,它现在制作18 = 20 ..但是在循环中它每次只增加1 ..所以下一行将是21 ..我需要它是22。

如果我刚刚执行此代码......'

$Dno = '18' + '2';



        while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
        {   

            //Description Binder from hcpc Table in DB
            while($ffdata=$qqq->fetch(PDO::FETCH_ASSOC))
            {
                    $dta =  $ffdata['code'];
                    $sq = "SELECT * FROM code WHERE code = :dta";
                    $qr=$con->prepare($sq);
                    $qr->bindvalue(":dta", $dta);
                    $qr->execute();
                    while($hcdata=$qr->fetch(PDO::FETCH_ASSOC))
                    {
                        $worksheet
                            ->setCellValue('C' . $Dno, $hcdata['description']);
                    }
            }

        }

它也使第一行= 20,但每行也将= 20

5 个答案:

答案 0 :(得分:3)

您使用$this += 2递增2。 ++只会增加一个。这就是运营商的定义方式。

从更一般的意义上讲,$this += x会增加x,因此您可以将增量定义为您想要的任何内容。

答案 1 :(得分:2)

你可以通过向它添加2来为变量添加2:

$this = $this + 2;

或:

$this += 2;
  

我仍然希望它以++结尾

似乎是随意的,但确定:

$this++;
$this++;

编辑:为了回应您答案中的更新代码,概念仍然相同。在这里,您在使用它时将值递增1:

$worksheet->setCellValue('C' . $Dno++, $hcdata['description']);

相反,在值中添加两个并使用它:

$Dno += 2;
$worksheet->setCellValue('C' . $Dno, $hcdata['description']);

或者,正如@Steve在下面的评论中所指出的那样,由于原作使用了帖子 -increment,因此您需要更改以下值:

$worksheet->setCellValue('C' . $Dno, $hcdata['description']);
$Dno += 2;

答案 2 :(得分:1)

使用此:

$this += 2;

或:

$this = $this+2;
顺便说一下,你首先分配了'18'(字符串),你应该使用整数18,当然PHP理解并将类型转换为整数,但更好的做法是使用正确的类型从一开始就避免额外的开销。

答案 3 :(得分:1)

您可以使用简单的自定义函数进行内联视图:

<?php
    function incByTwo( &$inp ) {
        $inp += 2;
        return $inp; // So you could use this inline
    }

    // Test function:
    $test_var = 1;

    for ($i = 0; $i < 5; $i++)
        print '<br />This is a test: '. incByTwo($test_var);

?>

<强>更新

现在你可以使用这样的东西:

while($hcdata=$qr->fetch(PDO::FETCH_ASSOC))
{
   $worksheet
       ->setCellValue('C' . incByTwo($Dno), $hcdata['description']);
...

请在使用前测试,我还没有。

答案 4 :(得分:1)

您无法链接++运算符:

php > $x = 7;
php > $x++++;
PHP Parse error:  syntax error, unexpected '++' (T_INC) in php shell code on line 1
php > ($x++)++;
PHP Parse error:  syntax error, unexpected '++' (T_INC) in php shell code on line 1

你留下了其他人所展示的选择:

$x += 2;
$x = $x + 2;