# Duncan's blog

## September 30, 2014

### Project Euler: problem 13 (PHP) – Large sum

Filed under: PHP,Project Euler — duncan @ 8:00 am

I previously blogged about this Project Euler puzzle 6 years ago, using ColdFusion.  This is my approach using PHP as a simple practical exercise for myself, and I’d appreciate any feedback on my PHP code.

Problem 13:

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
… etc.
53503534226472524250874054075591789781264330331690

Code:

```<?php
\$numbers = <<<FOO
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
[etc]
FOO;

\$sum = 0;

\$lines = explode("\r\n", \$numbers);

foreach(\$lines as \$line) {
\$sum += \$line;
}

echo \$sum;
```

So taking exactly the same approach as with ColdFusion.  And I get the same problem of having the number being output in scientific notation.  But again the first ten digits I need are visible without needing to do anything clever to output the entire number.  Which is a good job, as I’m not sure what I’d need to do!