Odd Echo在foreach循环后添加一个字符串

Hey guys been tearing my hair out for the last half hour cant understand an output im getting, somehow the variable outside the loop gets one value yet inside its working fine can anyone exlpain?

private function retrieve_jobs ($xml){
        $i = 0;
        $job_id= "";
                foreach ($xml->result->rowset->row as $row) {
               if($i==10) { break;}
               $job = "".$row['jobID'].",";
               echo $job;

               $job_id = $job_id + $job;
        }
        echo "<hr>";
        echo $job_id;

    }

returns

    222306493,221469738,221347167,192148888,192148812,192148779,191545019,191544687,191431978,191410960,191404449,191394324,137944351,137241312,135658237,135611078,135586553,86984622,86984602,74752623,74655215,74546907,73206908,73206874,73204769,72540887,72539881,72489215,70936404,70931768,70911740,70907088,70907076,70891956,70702643,70702624,70588927,70424205,70423994,70423605,70399767,70394685,70372461,70372325,70354216,70336955,70236410,70236385,70062221,70055569,70055424,70039478,69734908,69733842,69716985,69716342,69706388,62723392,56140265,55035686,55034424,55020755,54975618,54972666,54961222,

(Number Outside Loop) 6396595747 Not sure where this number is coming from it should be the values above.

Use dot . as a concatenation operator in php,

$job_id = $job_id . $job;
                  ^

Side Note: The number 6396595747 you getting outside the loop is summation of all the number you getting inside the loop.

In PHP, . is used for concatenation, not +.

E.G:

$job_id = $job_id . $job;

This also works for strings:

$str = "Hello";

$str .= ", how are you?";

echo $str; //Hello, how are you?

As the other users mentioned in PHP you should use dot for string concatenation.

Also in this situation you can use implode function which is much more clear to read instead of concatenating commas each time.

Example:

private function retrieve_jobs ($xml){
    $i = 0;
    $job_ids = array();

    foreach ($xml->result->rowset->row as $row) {
       if($i==10) { break;}
       $job_ids[] = $row['jobID'];
       echo $row['jobID'];
    }

    echo "<hr>";
    echo implode(', ', $job_ids);
}