Better way to add number to itself?

后端 未结 9 1399
心在旅途
心在旅途 2020-12-17 14:16

Is there a better/shorter way in PHP of doing

$x = $x + 10;

i.e.

something like

$x .= 10; // (but this doesn\'t add         


        
相关标签:
9条回答
  • 2020-12-17 14:42

    $x += 10;

    However some people find this harder to read.

    What you tried ($x.= 10) works only for strings.

    E.g.

    $x = 'test';
    $x.= 'ing...';
    
    0 讨论(0)
  • 2020-12-17 14:43
    $x+=10;
    

    Is this what you're wanting?

    0 讨论(0)
  • 2020-12-17 14:44

    it's pretty simple. $x += 10; is same as $x = $x + 10;

    0 讨论(0)
  • Like in many other languages:

    $x += 10;
    

    More information: Assignment Operators

    0 讨论(0)
  • 2020-12-17 14:53

    $x +=10; is equivalent to $x = $x +10;

    http://www.tizag.com/phpT/operators.php

    0 讨论(0)
  • 2020-12-17 15:03

    $x += 10; adds 10 to $x

    or

    $x += $x; adds $x to itself, but you could just do: $x *= 2;

    0 讨论(0)
提交回复
热议问题