LoginSignup
0
0

More than 1 year has passed since last update.

変数に数値を足す方法(PHP)

Last updated at Posted at 2023-04-07

初めに

今回は、変数に代入した値と数値を足す方法を記載する。

変数に数値を足すには

変数に数値を足す方法は下記の通り

記載例
$num1 = 3;

//num1にnum1+10を代入する。
$num1 = $num1 + 10;

echo $num1;
//結果:13が出力される。

また、変数に数値を足す形には、省略形がある。

記載例
$num1 = $num1 + 10;  $num1 += 10;
//その他の四則演算に関しても、同様に省略して書くことが可能

さらに、計算する数値が1の時だけ、さらに省略して記載することができる。
また、1を足す省略形で値を出力する際は、結果に注意する必要がある。

記載例
$num = $num + 1;
 $num += 1;
 $num++;


$x = 3;
$y = 3;
echo ++$x; //結果:4 echoの前に1が足される
echo $y++; //結果:3 echoの後に1が足される
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0