<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Calculate Common Factors</title>
</head>

<body bgcolor="white">
<h1>Calculate Common Factors PHP Program</h1>

<p><a href="index.html">Math pages home</a></p>

<form method="get" action="common_factors.php">
    Enter Numbers Separated by Commas: <input type="text" name="list" size="25" maxlength="100" value="<?php echo $_GET['list']; ?>" onfocus="this.value=''"> <input type="submit" value="Calculate"> <input type="reset" value="Clear Form">
</form>

<?php

// This is a comment (ignored by the programming language, but interesting for human readers)
/* This is also a comment */

// function to calculate the common factors of a list of numbers
// returns a string with the factors separated by commas too
function getCommonFactors($list) {
    
// turn the comma separated string called $list into an array of numbers called $allNumbers:
    
$allNumbers split(','$list);
    
// find the smallest of all the numbers
    // loop through each of the numbers in turn setting $num to each of them in the body of the loop:
    
foreach($allNumbers as $num) {
        if (
$num $smallest || !isset($smallest))
            
$smallest $num;
    }
    
// Test each of the numbers from 1 to $smallest to see if it is a common factor:
    
for($test 1$test <= $smallest$test++) {
        
$isCommon true// if we make it through the following loop without changing this, that is!
        
foreach($allNumbers as $num) {
            
// the "mod" function denoted by the percent sign character gives us the 
            // remainder when the 1st arg isd divided by the 2nd:
            
$remainder $num $test;
            if (
$remainder != 0) {
                
$isCommon false// 'flag' that $test is not a common factor after all
                
break; // this breaks out of the foreach() loop
            
}
        }
        if (
$isCommon) {
            
// add the test number to our list of common factors:
            // first if the list has been started, separate the last factor with a comma:
            
if (isset($common))
                
$common .= ", "// the .= operator means "append onto the end of the string"
            
$common .= $test// then append the new common factor itself
        
}
    }
    return(
$common);
}

/* When a form does a "get" action, it returns each of the named values (see the input tags) as values in the array $_GET where the indexes into the array are the names of the arguments. We only have one argument to this script, which is 'list'. So an example URL suffix could be: common_factors.php?list=60,90 */
if (isset($_GET['list'])) {
    
$list $_GET['list'];
    
$common getCommonFactors($list);
    echo 
"<p style=\"font-size: 24pt; \">The common factors of '$list' are '$common'</p>";
}

?>

<p>This page is a small demonstration of the PHP programming language. You could use it to check your math homework! </p>

<p>You may <a href="common_factors.phps">read the PHP program</a> that makes this page work to get a feel for computer programming! It contains about 70 lines of text, fewer than 30 of these actually calculates the common factors. Many of the 70 lines are comments to explain how PHP and this specific program works.</p>

</body>
</html>