Help Example: Three
one | two | three | four | five
► QUESTION: I was working on the first Project Euler problem:
Multiples of 3 and 5
Problem 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Here is my code, it seems like it should work but instead of the correct sum (which is: 233,168) I get zero. Can you help me figure out what is wrong?
<? $maxValue = 1000; $sum = 0; for($i = 0; $i < maxValue; $i ++) { if($i % 3 == 0 || $i % 5 == 0) $sum += $i; } printf("The sum is: %d\n", $sum); ?>
► ANSWER:The problem with your code is here:
for($i = 0; $i < maxValue; $i ++) ^^^^^^^^
In php, all variables are prefixed with a '$', earlier in your program you had:
$maxValue = 1000;
Which does prefix 'maxValue' with a '$' but when it is missing, php interprets things differently. To understand what is going on, consider this piece of code:
<? define("forty_two", 42); printf("The value of the define 'forty_two' is: %d\n", forty_two); ?>
When executed, this code produces:
The value of the define 'forty_two' is: 42
The feature being used is php's "define" feature which lets you create a named constant. Notice that when the constant is used, it is *not* preceded by a '$' (which makes sense as a 'define' is not a variable).
Alright, so when php sees a "thing" without a '$', one possible interpretation is that that "thing" is a named constant. In your code though, you never defined a constant 'maxValue' (only a variable) so trying to use that "constant" confused php and it defaults to using zero for any undefined, named constant. This is why your code says that '$sum' is zero, because the code (to php) reads as if it said:
<? $maxValue = 1000; $sum = 0; for($i = 0; $i < 0; $i ++) { if($i % 3 == 0 || $i % 5 == 0) $sum += $i; } printf("The sum is: %d\n", $sum); ?>
Ok, so adding '$' before 'maxValue' in the for loop will fix it, but this is a bit annoying isn't it? The program ran and produced output but in no way warned you that you might be doing something a little odd.
As it turns out, php does have a way to address this - here's your same program with one small addition:
<? error_reporting(-1); $maxValue = 1000; $sum = 0; for($i = 0; $i < maxValue; $i ++) { if($i % 3 == 0 || $i % 5 == 0) $sum += $i; } printf("The sum is: %d\n", $sum); ?>
When executed, the code above outputs:
Notice: Use of undefined constant maxValue - assumed 'maxValue' in euler.php on line 8 The sum is: 0
Much better! It not only informed us of the issue but told us the precise line number where it occurred. So what does 'error_reporting(-1)' do? Php has a system of flags it uses to allow you to select which errors it informs you of. They use named constants (like 'E_ERROR' or 'E_WARNING') and provide fine-grained control over php's error reporting output.
So why do we pass -1 to error_reporting? Because error_reporting's constants are all bit flags and -1 is a number with *every* bit set. So, saying:
error_reporting(-1);
Tells php:
"Inform me of every possible error and warning that you know about"
While this might *seem* extreme, the errors and warnings you encounter as a consequence of using hightened error_reporting levels will almost be legitimate issues that need to be corrected.