Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to add number to itself?

Tags:

variables

php

Is there a better/shorter way in PHP of doing

$x = $x + 10;

i.e.

something like

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

I am sure i've seen a shorter way than doing $x = $x + 10;

like image 495
Lee Avatar asked Jun 27 '11 16:06

Lee


4 Answers

Have a look at: http://php.net/manual/language.operators.assignment.php (in the code examples and comments)

You can use:

$x += 10;

for example.

like image 181
Yoshi Avatar answered Nov 03 '22 01:11

Yoshi


Not a PHP guy, but $x += 10; maybe?

like image 36
evilone Avatar answered Nov 03 '22 00:11

evilone


$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...';
like image 13
PeeHaa Avatar answered Nov 03 '22 00:11

PeeHaa


Like in many other languages:

$x += 10;

More information: Assignment Operators

like image 8
Felix Kling Avatar answered Nov 03 '22 01:11

Felix Kling