LoginSignup
0
0

More than 5 years have passed since last update.

【PHP】数字の文字列に対する総和を計算して文字列にして返す

Last updated at Posted at 2019-04-09

数字の文字列に対する総和を計算して文字列にして返す

んーreturn (string)$res;の型変換が蛇足感

// 数字の文字列に対する総和を計算する
function calculateSum(string $param) : string
{
    $su = (int)$param;
    $res = 0;
    for ($i = 1; $i <= $su; $i++) {
        $res += $i;
    }

    if ($su <= 0) {
        return "ERROR";
    }

    return (string)$res;
}

※0以下はエラーを返す

ソース

0
0
0

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