<?php
/**
 * Author: David Mottern
 * This was inspired by Project Euler problem 2.
 * https://projecteuler.net/problem=2
 * It's not meant to be a final polished project.
 * 
 * Even Fibonacci numbers
 * Problem 2
 * Each new term in the Fibonacci sequence is generated by adding the previous two terms.
 * By starting with 1 and 2, the first 10 terms will be:
 *             1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
 * By considering the terms in the Fibonacci sequence whose values do not exceed four million,
 * find the sum of the even-valued terms.
 * 
 * I've written a more general solution where you can pass any limit,
 * and an optional parameter can be used to choose between finding the sum
 * of even or odd terms (defaulting to even).
 * This is live on the web at http://mottern.com/projecteuler/Problem2/
 * To solve for the problem described, for example, you would call it as follows:
 *         http://mottern.com/projecteuler/Problem2/?limit=4000000
 * This will also work if the values are sent in a Post rather than a Get.
 */
 
    
function sum_of_fibonaccis($limit$oddeven='even') {
        
/*
         * Sum all even or odd Fibonacci numbers less than $limit.
         * 
         * @param int $limit
         *         Add all even or odd Fibonacci numbers that are less than this.
         * 
         * @param optional string $oddeven
         *         Determines whether to add odd or even numbers.
         * 
         * @return int
         *         The final sum.
         */
        
$sum 0;
        
$previous 1;
        for(
$n 1$n $limit$current $n$n += $previous$previous $current) {
            switch(
$oddeven) {
                case 
'odd':
                    if (
$n 0) {
                        
$sum += $n;
                    }
                    break;
                case 
'even':
                default:
                    if (
$n == 0) {
                        
$sum += $n;
                    }
                    break;
            }
            
        }
        return 
$sum;
    }

    
set_error_handler("warning_handler"E_WARNING); // Show usage message for both errors and warnings.

    
function warning_handler() {
        throw new 
Exception();
    }

    
// Main Code
       
try {
           if (isset(
$_REQUEST['show_source'])) {
            
show_source(__FILE__);
            exit;
        }
           if (!isset(
$_REQUEST['limit'])) {
               throw new 
Exception();
        }
           
$limit $_REQUEST['limit'];
        if (isset(
$_REQUEST['oddeven'])) {
            
$oddeven $_REQUEST['oddeven'];
        } else {
            
$oddeven 'even';
        }
        echo 
sum_of_fibonaccis($limit$oddeven);
    } catch (
Exception $e) {
//        Show the usage message regardless of the exception type.
        
echo 'usage: <i>index.php?limit=x&oddeven=even</i> with limit being an integer value > 0,
              and oddeven = either "odd" or "even" or omitted and defaulting to even.'
;
        echo 
'<br>View PHP source: <A href="http://mottern.com/projecteuler/Problem2/?show_source">http://mottern.com/projecteuler/Problem2/?show_source</A>';
    }
?>