setValue(20); setValue(); // will use the default value of 50 setValue(100); setValue(60); /* * PHP Functions - Returning values * * Using return statement * * */ echo "<br>"; function calculation($x, $y, $type) { if ($type == "+") { return $x + $y; } elseif ($type == "-") { return $x - $y; } elseif ($type == "*") { return $x * $y; } elseif ($type == "/" && $y != 0) { return $x / $y; } else { return "Error, calculation unknown"; } } echo "100 + 10 = " . calculation(100, 10, '+') . "<br>"; echo "211 - 11 = " . calculation(211, 11, "-") . "<br>"; echo "2 * 4 = " . calculation(2, 4, "*") . "<br>"; echo "3 / 4 = " . calculation(3, 4, "/") . "<br>";
<select name="op"> <?php foreach ($operators as $operator) { ?> <option <?php if (isset($op) && $operator == $op) { echo 'selected="selected"'; } ?> > <?php echo $operator; ?> </option> <?php } ?> </select> <input type="text" name="b" value="<?php echo $b; ?> "> <input type="submit" value="="> <?php if (isset($a) && isset($b) && isset($op)) { echo calculation($a, $b, $op); } ?> </form> </body> </html>
} $number *= $value; } } if ($operation === '/') { $number = $arr[0]; foreach ($arr as $value) { if ($value === $arr[0]) { continue; } $number /= $value; } } return $number; } echo calculation([8, 1, 2], '+'); /* ------------------- Home 3 ---------------------- */ echo '<p>-------- Third exercise -------</p>'; function calculate($operation) { $number = 0; $arr = func_get_args(); if ($operation === '+') { foreach ($arr as $value) { if ($arr[0] === '+' && key($arr) != 1) { continue; } $number += $value; } } if ($operation === '-') {
<?php for ($i = 0; $i < 10; $i++) { $a = rand(1, 50); $ha = rand(1, 20); $s = calculation($a, $ha); if ($s < 10) { echo "<div style='color:green'>Лицето на триъгълник със страна {$a} cm. и височина към нея {$ha} cm. е равно на {$s} квадратни сантиметра</div>"; } if ($s >= 10 && $s <= 23) { echo "<div style='color:red'>Лицето на триъгълник със страна {$a} cm. и височина към нея {$ha} cm. е равно на {$s} квадратни сантиметра</div>"; } if ($s > 23) { echo "<div style='color:blue'>Лицето на триъгълник със страна {$a} cm. и височина към нея {$ha} cm. е равно на {$s} квадратни сантиметра</div>"; } } function calculation($a, $ha) { $s = $a * $ha / 2; return $s; }
global $len, $arr; echo "Array("; for ($i = 0; $i < $len; $i++) { if ($arr[$i] % 2 == 0) { $value = $arr[$i]; $even_arr[] = $arr[$i]; echo $arr[$i] . ","; } } echo ")"; echo pr($even_arr); } echo "Even numbers are:...<br>"; echo even_number(); echo "The minimum number is " . $min . "<br>"; echo "The maximum bumber is " . $max . "<br><br><br>"; function calculation($x, $y) { $addition = $x + $y; $subtraction = $y - $x; $multiplication = $x * $y; $divided = $y / $x; $modulas = $y % $x; echo "The addition of " . $x . " and " . $y . " is: " . $addition . "<br>"; echo "The subtraction of " . $x . " and " . $y . " is: " . $subtraction . "<br>"; echo "The multiplication of " . $x . " and " . $y . " is: " . $multiplication . "<br>"; echo "The divided of " . $x . " and " . $y . " is: " . $divided . "<br>"; echo "The modulas of " . $x . " and " . $y . " is: " . $modulas . "<br>"; } echo calculation(2, 524);
for ($i = 0; $i < count($firstArr); $i++) { $res[$i] = $firstArr[$i] / $secondArr[$i]; } } foreach ($res as $value) { echo $value . ' '; } return $res; } calculation([8, 1, 2], [3, 5, 6], '+'); echo '<br>'; calculation([8, 1, 2], [3, 5, 6], '-'); echo '<br>'; calculation([8, 1, 2], [3, 5, 6], '*'); echo '<br>'; calculation([9, 5, 12], [3, 5, 6], '/'); /* ------------------- Home 3 ---------------------- */ /* Функция, принимающая переменное число аргументов, но последний аргумент обязательно должна быть строка, обозначающее арифметическое действие, которое необходимо выполнить со всеми передаваемыми аргументами. */ echo '<p>-------- Third exercise -------</p>'; function calculate() { $number = 0; $arr = func_get_args(); $lastArg = count($arr) - 1; $operation = $arr[$lastArg]; if ($operation === '+') {
function show() { global $LNG, $USER, $PLANET, $resource, $reslist, $CONF; //if($USER['id'] != 1){ //$this->printMessage('The page is under maintencane!', true, array('game.php?page=overview', 3)); //} $TOTAL = calculationBis(1101, $USER['academy_1101']) + calculationBis(1102, $USER['academy_1102']) + calculationBis(1103, $USER['academy_1103']) + calculationBis(1104, $USER['academy_1104']) + calculationBis(1105, $USER['academy_1105']) + calculationBis(1106, $USER['academy_1106']) + calculationBis(1107, $USER['academy_1107']) + calculationBis(1108, $USER['academy_1108']) + calculationBis(1109, $USER['academy_1109']) + calculationBis(1110, $USER['academy_1110']) + calculationBis(1111, $USER['academy_1111']) + calculationBis(1112, $USER['academy_1112']) + calculationBis(1113, $USER['academy_1113']) + calculationBis(1201, $USER['academy_1201']) + calculationBis(1202, $USER['academy_1202']) + calculationBis(1203, $USER['academy_1203']) + calculationBis(1204, $USER['academy_1204']) + calculationBis(1208, $USER['academy_1208']) + calculationBis(1209, $USER['academy_1209']) + calculationBis(1210, $USER['academy_1210']) + calculationBis(1205, $USER['academy_1205']) + calculationBis(1207, $USER['academy_1207']) + calculationBis(1206, $USER['academy_1206']) + calculationBis(1301, $USER['academy_1301']) + calculationBis(1302, $USER['academy_1302']) + calculationBis(1303, $USER['academy_1303']) + calculationBis(1304, $USER['academy_1304']) + calculationBis(1305, $USER['academy_1305']) + calculationBis(1306, $USER['academy_1306']) + calculationBis(1307, $USER['academy_1307']) + calculationBis(1308, $USER['academy_1308']) + calculationBis(1309, $USER['academy_1309']) + calculationBis(1310, $USER['academy_1310']) + calculationBis(1311, $USER['academy_1311']) + calculationBis(1312, $USER['academy_1312']) + calculationBis(1313, $USER['academy_1313']) + calculationBis(1314, $USER['academy_1314']); $academy_cost = 50; if ($CONF['academy_bonus'] != 0 && $CONF['purchase_bonus_timer'] > TIMESTAMP) { $academy_cost = round($academy_cost - $academy_cost / 100 * $CONF['academy_bonus']); } $academy_1101_bonus = getbonusOne(1101, $USER['academy_1101']); $academy_1101_points = calculation(1101, $USER['academy_1101']); $academy_1101_next = 1 + $USER['academy_1101'] * 1; $academy_1102_bonus1 = getbonusOne(1102, $USER['academy_1102']); $academy_1102_bonus2 = getbonusTwo(1102, $USER['academy_1102']); $academy_1102_points = calculation(1102, $USER['academy_1102']); $academy_1102_next = 1 + $USER['academy_1102'] * 1; $academy_1103_bonus = getbonusOne(1103, $USER['academy_1103']); $academy_1103_points = calculation(1103, $USER['academy_1103']); $academy_1103_next = 1 + $USER['academy_1103'] * 1; $academy_1104_bonus = getbonusOne(1104, $USER['academy_1104']); $academy_1104_points = calculation(1104, $USER['academy_1104']); $academy_1104_next = 1 + $USER['academy_1104'] * 1; $academy_1105_bonus = getbonusOne(1105, $USER['academy_1105']); $academy_1105_points = calculation(1105, $USER['academy_1105']); $academy_1105_next = 1 + $USER['academy_1105'] * 1; $academy_1106_bonus = getbonusOne(1106, $USER['academy_1106']); $academy_1106_points = calculation(1106, $USER['academy_1106']); $academy_1106_next = 1 + $USER['academy_1106'] * 1; $academy_1107_bonus = getbonusOne(1107, $USER['academy_1107']); $academy_1107_points = calculation(1107, $USER['academy_1107']); $academy_1107_next = 1 + $USER['academy_1107'] * 1; $academy_1108_bonus = getbonusOne(1108, $USER['academy_1108']); $academy_1108_points = calculation(1108, $USER['academy_1108']); $academy_1108_next = 1 + $USER['academy_1108'] * 1; $academy_1109_bonus = getbonusOne(1109, $USER['academy_1109']); $academy_1109_points = calculation(1109, $USER['academy_1109']); $academy_1109_next = 1 + $USER['academy_1109'] * 1; $academy_1110_bonus = getbonusOne(1109, $USER['academy_1110']); $academy_1110_points = calculation(1109, $USER['academy_1110']); $academy_1110_next = 1 + $USER['academy_1110'] * 1; $academy_1111_bonus = getbonusOne(1111, $USER['academy_1111']); $academy_1111_points = calculation(1111, $USER['academy_1111']); $academy_1111_next = 1 + $USER['academy_1111'] * 1; $academy_1112_bonus = getbonusOne(1112, $USER['academy_1112']); $academy_1112_points = calculation(1112, $USER['academy_1112']); $academy_1112_next = 1 + $USER['academy_1112'] * 1; $academy_1113_bonus = getbonusOne(1113, $USER['academy_1113']); $academy_1113_points = calculation(1113, $USER['academy_1113']); $academy_1113_next = 1 + $USER['academy_1113'] * 1; $academy_1201_bonus = getbonusOne(1201, $USER['academy_1201']); $academy_1201_points = calculation(1201, $USER['academy_1201']); $academy_1201_next = 1 + $USER['academy_1201'] * 1; $academy_1202_bonus = getbonusOne(1202, $USER['academy_1202']); $academy_1202_points = calculation(1202, $USER['academy_1202']); $academy_1202_next = 1 + $USER['academy_1202'] * 1; $academy_1203_bonus = getbonusOne(1203, $USER['academy_1203']); $academy_1203_points = calculation(1203, $USER['academy_1203']); $academy_1203_next = 1 + $USER['academy_1203'] * 1; $academy_1204_bonus = getbonusOne(1204, $USER['academy_1204']); $academy_1204_points = calculation(1204, $USER['academy_1204']); $academy_1204_next = 1 + $USER['academy_1204'] * 1; $academy_1205_bonus = getbonusOne(1205, $USER['academy_1205']); $academy_1205_points = calculation(1205, $USER['academy_1205']); $academy_1205_next = 1 + $USER['academy_1205'] * 1; $academy_1206_bonus = getbonusOne(1206, $USER['academy_1206']); $academy_1206_points = calculation(1206, $USER['academy_1206']); $academy_1206_next = 1 + $USER['academy_1206'] * 1; $academy_1207_bonus = getbonusOne(1207, $USER['academy_1207']); $academy_1207_points = calculation(1207, $USER['academy_1207']); $academy_1207_next = 1 + $USER['academy_1207'] * 1; $academy_1208_bonus = getbonusOne(1208, $USER['academy_1208']); $academy_1208_points = calculation(1208, $USER['academy_1208']); $academy_1208_next = 1 + $USER['academy_1208'] * 1; $academy_1209_bonus = getbonusOne(1209, $USER['academy_1209']); $academy_1209_points = calculation(1209, $USER['academy_1209']); $academy_1209_next = 1 + $USER['academy_1209'] * 1; $academy_1210_bonus = getbonusOne(1210, $USER['academy_1210']); $academy_1210_points = calculation(1210, $USER['academy_1210']); $academy_1210_next = 1 + $USER['academy_1210'] * 1; $academy_1301_bonus = getbonusOne(1301, $USER['academy_1301']); $academy_1301_points = calculation(1301, $USER['academy_1301']); $academy_1301_next = 1 + $USER['academy_1301'] * 1; $academy_1302_bonus1 = getbonusOne(1302, $USER['academy_1302']); $academy_1302_bonus2 = getbonusTwo(1302, $USER['academy_1302']); $academy_1302_points = calculation(1302, $USER['academy_1302']); $academy_1302_next = 1 + $USER['academy_1302'] * 1; $academy_1303_bonus = getbonusOne(1303, $USER['academy_1303']); $academy_1303_points = calculation(1303, $USER['academy_1303']); $academy_1303_next = 1 + $USER['academy_1303'] * 1; $academy_1304_bonus = getbonusOne(1304, $USER['academy_1304']); $academy_1304_points = calculation(1304, $USER['academy_1304']); $academy_1304_next = 1 + $USER['academy_1304'] * 1; $academy_1305_bonus = getbonusOne(1305, $USER['academy_1305']); $academy_1305_points = calculation(1305, $USER['academy_1305']); $academy_1305_next = 1 + $USER['academy_1305'] * 1; $academy_1306_bonus = getbonusOne(1306, $USER['academy_1306']); $academy_1306_points = calculation(1306, $USER['academy_1306']); $academy_1306_next = 1 + $USER['academy_1306'] * 1; $academy_1307_bonus = getbonusOne(1307, $USER['academy_1307']); $academy_1307_points = calculation(1307, $USER['academy_1307']); $academy_1307_next = 1 + $USER['academy_1307'] * 1; $academy_1308_bonus = getbonusOne(1308, $USER['academy_1308']); $academy_1308_points = calculation(1308, $USER['academy_1308']); $academy_1308_next = 1 + $USER['academy_1308'] * 1; $academy_1309_bonus = getbonusOne(1309, $USER['academy_1309']); $academy_1309_points = calculation(1309, $USER['academy_1309']); $academy_1309_next = 1 + $USER['academy_1309'] * 1; $academy_1310_bonus = getbonusOne(1310, $USER['academy_1310']); $academy_1310_points = calculation(1310, $USER['academy_1310']); $academy_1310_next = 1 + $USER['academy_1310'] * 1; $academy_1311_bonus = getbonusOne(1311, $USER['academy_1311']); $academy_1311_points = calculation(1311, $USER['academy_1311']); $academy_1311_next = 1 + $USER['academy_1311'] * 1; $academy_1312_bonus = getbonusOne(1312, $USER['academy_1312']); $academy_1312_points = calculation(1312, $USER['academy_1312']); $academy_1312_next = 1 + $USER['academy_1312'] * 1; $academy_1313_bonus = getbonusOne(1313, $USER['academy_1313']); $academy_1313_points = calculation(1313, $USER['academy_1313']); $academy_1313_next = 1 + $USER['academy_1313'] * 1; $academy_1314_bonus = getbonusOne(1314, $USER['academy_1314']); $academy_1314_points = calculation(1314, $USER['academy_1314']); $academy_1314_next = 1 + $USER['academy_1314'] * 1; $manual_25_step = 1; if ($USER['training'] == 0 && $USER['training_step'] == 25 && $USER['academy_1101'] < 1 && $USER['academy_1201'] < 1 && $USER['academy_1301'] < 1) { $manual_25_step = 0; } $manual_25_1_step = 1; if ($USER['training'] == 0 && $USER['training_step'] == 25 && ($USER['academy_1101'] >= 1 || $USER['academy_1201'] >= 1 || $USER['academy_1301'] >= 1)) { $manual_25_1_step = 0; $GLOBALS['DATABASE']->query("UPDATE " . USERS . " SET `training_step` = '26' WHERE `id` = " . $USER['id'] . ";"); $GLOBALS['DATABASE']->query("UPDATE " . USERS . " SET `experience_peace` = `experience_peace` + '650' WHERE `id` = " . $USER['id'] . ";"); } $this->tplObj->loadscript("pointes.js"); $this->tplObj->assign_vars(array('manual_25_step' => $manual_25_step, 'manual_25_1_step' => $manual_25_1_step, 'academy_p' => pretty_number($USER['academy_p']), 'academy_1101' => $USER['academy_1101'], 'academy_1102' => $USER['academy_1102'], 'academy_1103' => $USER['academy_1103'], 'academy_1104' => $USER['academy_1104'], 'academy_1105' => $USER['academy_1105'], 'academy_1106' => $USER['academy_1106'], 'academy_1107' => $USER['academy_1107'], 'academy_1108' => $USER['academy_1108'], 'academy_1109' => $USER['academy_1109'], 'academy_1110' => $USER['academy_1110'], 'academy_1111' => $USER['academy_1111'], 'academy_1112' => $USER['academy_1112'], 'academy_1113' => $USER['academy_1113'], 'academy_1201' => $USER['academy_1201'], 'academy_1202' => $USER['academy_1202'], 'academy_1203' => $USER['academy_1203'], 'academy_1204' => $USER['academy_1204'], 'academy_1208' => $USER['academy_1208'], 'academy_1209' => $USER['academy_1209'], 'academy_1210' => $USER['academy_1210'], 'academy_1205' => $USER['academy_1205'], 'academy_1207' => $USER['academy_1207'], 'academy_1206' => $USER['academy_1206'], 'academy_1301' => $USER['academy_1301'], 'academy_1302' => $USER['academy_1302'], 'academy_1303' => $USER['academy_1303'], 'academy_1304' => $USER['academy_1304'], 'academy_1305' => $USER['academy_1305'], 'academy_1306' => $USER['academy_1306'], 'academy_1307' => $USER['academy_1307'], 'academy_1308' => $USER['academy_1308'], 'academy_1309' => $USER['academy_1309'], 'academy_1310' => $USER['academy_1310'], 'academy_1311' => $USER['academy_1311'], 'academy_1312' => $USER['academy_1312'], 'academy_1313' => $USER['academy_1313'], 'academy_1314' => $USER['academy_1314'], 'academy_1101_bonus' => $academy_1101_bonus, 'academy_1101_points' => $academy_1101_points, 'academy_1101_next' => $academy_1101_next, 'academy_1102_bonus1' => $academy_1102_bonus1, 'academy_1102_bonus2' => $academy_1102_bonus2, 'academy_1102_points' => $academy_1102_points, 'academy_1102_next' => $academy_1102_next, 'academy_1103_bonus' => $academy_1103_bonus, 'academy_1103_points' => $academy_1103_points, 'academy_1103_next' => $academy_1103_next, 'academy_1104_bonus' => $academy_1104_bonus, 'academy_1104_points' => $academy_1104_points, 'academy_1104_next' => $academy_1104_next, 'academy_1105_bonus' => $academy_1105_bonus, 'academy_1105_points' => $academy_1105_points, 'academy_1105_next' => $academy_1105_next, 'academy_1106_bonus' => $academy_1106_bonus, 'academy_1106_points' => $academy_1106_points, 'academy_1106_next' => $academy_1106_next, 'academy_1107_bonus' => $academy_1107_bonus, 'academy_1107_points' => $academy_1107_points, 'academy_1107_next' => $academy_1107_next, 'academy_1108_bonus' => $academy_1108_bonus, 'academy_1108_points' => $academy_1108_points, 'academy_1108_next' => $academy_1108_next, 'academy_1109_bonus' => $academy_1109_bonus, 'academy_1109_points' => $academy_1109_points, 'academy_1109_next' => $academy_1109_next, 'academy_1110_bonus' => $academy_1110_bonus, 'academy_1110_points' => $academy_1110_points, 'academy_1110_next' => $academy_1110_next, 'academy_1111_bonus' => $academy_1111_bonus, 'academy_1111_points' => $academy_1111_points, 'academy_1111_next' => $academy_1111_next, 'academy_1112_bonus' => $academy_1112_bonus, 'academy_1112_points' => $academy_1112_points, 'academy_1112_next' => $academy_1112_next, 'academy_1113_bonus' => $academy_1113_bonus, 'academy_1113_points' => $academy_1113_points, 'academy_1113_next' => $academy_1113_next, 'academy_1201_bonus' => $academy_1201_bonus, 'academy_1201_points' => $academy_1201_points, 'academy_1201_next' => $academy_1201_next, 'academy_1202_bonus' => $academy_1202_bonus, 'academy_1202_points' => $academy_1202_points, 'academy_1202_next' => $academy_1202_next, 'academy_1203_bonus' => $academy_1203_bonus, 'academy_1203_points' => $academy_1203_points, 'academy_1203_next' => $academy_1203_next, 'academy_1204_bonus' => $academy_1204_bonus, 'academy_1204_points' => $academy_1204_points, 'academy_1204_next' => $academy_1204_next, 'academy_1205_bonus' => $academy_1205_bonus, 'academy_1205_points' => $academy_1205_points, 'academy_1205_next' => $academy_1205_next, 'academy_1206_bonus' => $academy_1206_bonus, 'academy_1206_points' => $academy_1206_points, 'academy_1206_next' => $academy_1206_next, 'academy_1207_bonus' => $academy_1207_bonus, 'academy_1207_points' => $academy_1207_points, 'academy_1207_next' => $academy_1207_next, 'academy_1208_bonus' => $academy_1208_bonus, 'academy_1208_points' => $academy_1208_points, 'academy_1208_next' => $academy_1208_next, 'academy_1209_bonus' => $academy_1209_bonus, 'academy_1209_points' => $academy_1209_points, 'academy_1209_next' => $academy_1209_next, 'academy_1210_bonus' => $academy_1210_bonus, 'academy_1210_points' => $academy_1210_points, 'academy_1210_next' => $academy_1210_next, 'academy_1301_bonus' => $academy_1301_bonus, 'academy_1301_points' => $academy_1301_points, 'academy_1301_next' => $academy_1301_next, 'academy_1302_bonus1' => $academy_1302_bonus1, 'academy_1302_bonus2' => $academy_1302_bonus2, 'academy_1302_points' => $academy_1302_points, 'academy_1302_next' => $academy_1302_next, 'academy_1303_bonus' => $academy_1303_bonus, 'academy_1303_points' => $academy_1303_points, 'academy_1303_next' => $academy_1303_next, 'academy_1304_bonus' => $academy_1304_bonus, 'academy_1304_points' => $academy_1304_points, 'academy_1304_next' => $academy_1304_next, 'academy_1305_bonus' => $academy_1305_bonus, 'academy_1305_points' => $academy_1305_points, 'academy_1305_next' => $academy_1305_next, 'academy_1306_bonus' => $academy_1306_bonus, 'academy_1306_points' => $academy_1306_points, 'academy_1306_next' => $academy_1306_next, 'academy_1307_bonus' => $academy_1307_bonus, 'academy_1307_points' => $academy_1307_points, 'academy_1307_next' => $academy_1307_next, 'academy_1308_bonus' => $academy_1308_bonus, 'academy_1308_points' => $academy_1308_points, 'academy_1308_next' => $academy_1308_next, 'academy_1309_bonus' => $academy_1309_bonus, 'academy_1309_points' => $academy_1309_points, 'academy_1309_next' => $academy_1309_next, 'academy_1310_bonus' => $academy_1310_bonus, 'academy_1310_points' => $academy_1310_points, 'academy_1310_next' => $academy_1310_next, 'academy_1311_bonus' => $academy_1311_bonus, 'academy_1311_points' => $academy_1311_points, 'academy_1311_next' => $academy_1311_next, 'academy_1312_bonus' => $academy_1312_bonus, 'academy_1312_points' => $academy_1312_points, 'academy_1312_next' => $academy_1312_next, 'academy_1313_bonus' => $academy_1313_bonus, 'academy_1313_points' => $academy_1313_points, 'academy_1313_next' => $academy_1313_next, 'academy_1314_bonus' => $academy_1314_bonus, 'academy_1314_points' => $academy_1314_points, 'academy_1314_next' => $academy_1314_next, 'academy_cost' => $academy_cost, 'total_free' => floor($TOTAL / 2), 'total_dm' => floor($TOTAL / 100 * 75), 'total_am' => floor($TOTAL))); $this->display("page.academy.default.tpl"); }
<?php function calculation($a, $word) { $num = strlen($word); if ($num % 2) { $a += 12; } else { $a -= 3; } if ($a > 10) { $a -= 22; } if ($a < 0) { $a += 14; } echo $a; echo "<br>"; } echo calculation(25, 'error'); echo calculation(25, 'second'); echo calculation(25, 'php'); echo calculation(25, 'pokemon'); echo calculation(25, 'Screenshot'); echo calculation(25, 'Pneumonoultramicroscopicsilicovolcanoconiosis');