function Sum() { $numargs = func_num_args(); $result = 0; for ($i = 0; $i < $numargs; $i++) { $arg = func_get_arg($i); if (is_numeric($arg)) { $result += $arg; } else { if (get_class($arg) == "XLLoop_XLArray") { $c = count($arg->array); for ($j = 0; $j < $c; $j++) { $result += Sum($arg->array[$j]); } } } } return $result; }
function StdDev($x) { foreach ($x as $val) { $x2 = pow($val, 2); $sumx2 += $x2; } if (count($x) != 0) { $a = pow(Sum($x), 2) / count($x); } $b = $sumx2 - $a; if (count($x) != 1) { $c = $b / (count($x) - 1); } return pow($c, 0.5); }
[expect php] [file] <?php $a = 1; $b = 2; function Sum() { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; } Sum(); echo $b;
function Average($arr) { $sum = Sum($arr); $num = count($arr); return $sum / $num; }
function showForm($MenuItem) { #the table header echo '<form action="' . THIS_PAGE . '" method="post"> <table style="width:100%"> <tr><th class="left">Quantity</th><th class="left">Biscuit</th><th class="left">Description</th><th class="right">Price Each</th><th>Extra Gravy</th><th>Meal-Size-It</th><th class="right">Totals</th><tr> '; #MENU LIST - this part allows for a dyamic menu $i = 0; #i is for itteration foreach ($MenuItem as $key => $value) { #create the arrays that represents quantity, xtrGravy, xtrMeal #$arr[0]=name [0]=value (number or state=checked/unchecked) $quantity[$i][0] = "quantity" . $i; $xtrGravy[$i][0] = "xtrGravy" . $i; $xtrMeal[$i][0] = "xtrMeal" . $i; #Set values - here we have to differenciate between a first presence or a post back: if (isset($_POST['quantity' . $i])) { #post back $quantity[$i][1] = $_POST['quantity' . $i]; } else { #first presence $quantity[$i][1] = 0; } #end of differenciation if (isset($_POST['xtrGravy' . $i])) { #post back $xtrGravy[$i][1] = "checked"; $xtrGravy[$i][2] = GRAVY_PRICE; } else { #first presence $xtrGravy[$i][1] = ""; $xtrGravy[$i][2] = 0; } #end of differenciation if (isset($_POST['xtrMeal' . $i])) { #post back $xtrMeal[$i][1] = "checked"; $xtrMeal[$i][2] = MEAL_UPGRADE; } else { #first presence $xtrMeal[$i][1] = ""; $xtrMeal[$i][2] = 0; } #end of differenciation $menuTotal[$i] = $quantity[$i][1] * ($value->price + $xtrGravy[$i][2] + $xtrMeal[$i][2]); echo ' <tr> <td><input type="number" name="' . $quantity[$i][0] . '" min="0" max="99" value="' . $quantity[$i][1] . '" /></td> <td>' . $value->name . '</td> <td>' . $value->description . '</td> <td class="right">' . $value->price . '</td> <td><input type="checkbox" name="' . $xtrGravy[$i][0] . '" min="0" max="99" value=""' . $xtrGravy[$i][1] . ' />$0.50 each</td> <td><input type="checkbox" name="' . $xtrMeal[$i][0] . '" min="0" max="99" value=""' . $xtrMeal[$i][1] . ' />$3.00 each</td> <td class="right">' . money_format('%(#10n', $menuTotal[$i]) . '</td> </tr> '; $i++; } # for the extras we again have to differentiate between a postback and first presence $xtraTotal = 0.0; if (isset($_POST['cbGravy'])) { $cbGravy = "checked"; $xtraTotal += GRAVY_PRICE; } else { $cbGravy = ""; } if (isset($_POST['cbUpGrade'])) { $cbUpGrade = "checked"; $xtraTotal += MEAL_UPGRADE; } else { $cbUpGrade = ""; } #end of differentiation #this part of the calculation is always the same $subTotal = Sum($menuTotal) + $xtraTotal; $tax = $subTotal * 0.1; $total = $subTotal + $tax; #the totals part - starting with the subtotal ending with the grand total. echo '<tr><td colspan="5"></td><td class="right topline"><b>SubTotal:</b></td><td class="right topline">' . money_format('%(#10n', $subTotal) . '</td></tr> <tr><td colspan="5"></td><td class="right bottomline"><b>Tax:</b></td><td class="right bottomline">' . money_format('%(#10n', $tax) . '</td></tr> <tr><td colspan = "5"></td><td class="right doublebottomline"><b>Total:</b><td class="right doublebottomline"><b>' . money_format('%(#10n', $total) . '</b></td></tr> '; #SUBMIT BUTTONS set submit buttons according to type of submission if (isset($_POST['order'])) { // buttons are hidden and thanks for order visible $visableSubmit = "hidden"; $visableConfirm = "hidden"; $visableThankYou = "<h3>Thanks for your order.</h3>"; } elseif (isset($_POST['submit'])) { // all buttons visible and thanks for order hidden $visableSubmit = ""; $visableConfirm = ""; $visableThankYou = ""; $re = "Re-"; } else { // just submit button visible $visableSubmit = ""; $visableConfirm = "hidden"; $visableThankYou = ""; $re = ""; } echo ' <tr><td colspan = "7"><input class="submit" type = "submit" name = "submit" value="' . $re . 'Calculate Price" style = "width:100%" ' . $visableSubmit . ' /></td></td></tr> <tr><td colspan = "7"><input class="submit" type = "submit" name = "order" value="Confirm Order" style = "width:100%" ' . $visableConfirm . ' /></td></td></tr> </table> </form> '; }
<?php #Example #1 Using global $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; $a = 10; return $b; } $d = Sum(); echo 'b ' . $b . "\n\n"; echo 'a ' . $a . "\n\n"; echo 'd ' . $d . "\n\n";
<?php header('Content-Type:text/html; charset=utf-8'); /* function Say(){ echo '<h3>我想你咯!</h3>'; } Say(); for($i=0; $i<10; $i++) Say(); */ //函数的返回值 function Sum($a, $b){ return $a + $b; } echo Sum(13, 74); echo '<br />PHP中函数不区分大小写'; echo sum(75, 88); ?>
<?php function Sum($New) { for ($i = 1; $i <= $New; $i += 2) { $sum += $i; } print $sum; } ?> <html> <head> <title>Test</title> </head> <body> <?php Sum(50); ?> </body> </html>