<?php function add($a, $b) { return $a + $b; } function subtract($a, $b) { return $a - $b; } function multiply($a, $b) { return $a * $b; // Add code here } function divide($a, $b) { return $a / $b; } // Add code to test your functions here echo add(4, 5); echo subtract(18, 9); echo multiply(3, 3); echo divide(18, 3);
public function __call($method, $parameters) { if (!defined('static::factory')) { throw new UndefinedConstException('[const factory] is required to use the [Call Factory Method Ability]!'); } $originMethodName = $method; $method = strtolower($method); $calledClass = get_called_class(); if (!isset(static::factory['methods'][$method])) { Support::classMethod($calledClass, $originMethodName); } $class = static::factory['methods'][$method]; $factory = static::factory['class'] ?? NULL; if ($factory !== NULL) { return $factory::class($class)->{$method}(...$parameters); } else { $classEx = explode('::', $class); $class = $classEx[0] ?? NULL; $method = $classEx[1] ?? NULL; $isThis = NULL; if (stristr($method, ':this')) { $method = str_replace(':this', NULL, $method); $isThis = 'this'; } $namespace = str_ireplace(divide($calledClass, '\\', -1), NULL, $calledClass); $return = uselib($namespace . $class)->{$method}(...$parameters); if ($isThis === 'this') { return $this; } return $return; } }
public function testTraverseFailureForArrayOfInt() { $dividend = 12; $divisors = [2, 0, 6]; $intsArray = new AssociativeArray($divisors); $instance = Either::left(''); $eitherResults = $intsArray->traverse(function ($i) use($dividend) { return divide($dividend, $i); }, $instance); $expected = Either::left('Division by zero!'); $this->assertEquals($expected, $eitherResults); }
public static function class(string $class) { $namespace = NULL; if (defined('static::namespace')) { $namespace = suffix(static::namespace, '\\'); } else { $calledClass = get_called_class(); $namespace = str_ireplace(divide($calledClass, '\\', -1), NULL, $calledClass); } $class = $namespace . $class; return uselib($class); }
function ipv4() : string { if (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = divide($_SERVER['HTTP_X_FORWARDED_FOR'], ','); } else { $ip = $_SERVER['REMOTE_ADDR']; } if ($ip === '::1') { $ip = '127.0.0.1'; } return $ip; }
public function __construct() { if (defined('static::connection')) { Config::set('Database', 'database', static::connection); } if (defined('static::table')) { $grandTable = static::table; } else { $grandTable = divide(str_ireplace([INTERNAL_ACCESS, 'Grand'], '', get_called_class()), '\\', -1); } $this->grandTable = strtolower($grandTable); $tables = DBTool::listTables(); if (!in_array($this->grandTable, Arrays::map('strtolower', $tables))) { throw new Exception(lang('Database', 'tableNotExistsError', 'Grand: ' . $grandTable)); } }
public function use(string $randomPageVariable, array $randomDataVariable = NULL, bool $randomObGetContentsVariable = false) { if (!empty(Properties::$parameters['usable'])) { $randomObGetContentsVariable = Properties::$parameters['usable']; } if (!empty(Properties::$parameters['data'])) { $randomDataVariable = Properties::$parameters['data']; } Properties::$parameters = []; $eol = EOL; $randomPageVariableExtension = extension($randomPageVariable); $randomPageVariableBaseUrl = baseUrl($randomPageVariable); $return = ''; if (!is_file($randomPageVariable)) { throw new InvalidArgumentException('Error', 'fileParameter', '1.($randomPageVariable)'); } if ($randomPageVariableExtension === 'js') { $return = '<script type="text/javascript" src="' . $randomPageVariableBaseUrl . '"></script>' . $eol; } elseif ($randomPageVariableExtension === 'css') { $return = '<link href="' . $randomPageVariableBaseUrl . '" rel="stylesheet" type="text/css" />' . $eol; } elseif (stristr('svg|woff|otf|ttf|' . implode('|', Config::get('ViewObjects', 'font')['differentFontExtensions']), $randomPageVariableExtension)) { $return = '<style type="text/css">@font-face{font-family:"' . divide(removeExtension($randomPageVariable), "/", -1) . '"; src:url("' . $randomPageVariableBaseUrl . '") format("truetype")}</style>' . $eol; } elseif ($randomPageVariableExtension === 'eot') { $return = '<style type="text/css"><!--[if IE]>@font-face{font-family:"' . divide(removeExtension($randomPageVariable), "/", -1) . '"; src:url("' . $randomPageVariableBaseUrl . '") format("truetype")}<![endif]--></style>' . $eol; } else { $randomPageVariable = suffix($randomPageVariable, '.php'); if (is_file($randomPageVariable)) { if (is_array($randomDataVariable)) { extract($randomDataVariable, EXTR_OVERWRITE, 'zn'); } if ($randomObGetContentsVariable === false) { require $randomPageVariable; } else { ob_start(); require $randomPageVariable; $randomSomethingFileContent = ob_get_contents(); ob_end_clean(); return $randomSomethingFileContent; } } } if ($randomObGetContentsVariable === false) { echo $return; } else { return $return; } }
/** * @param array $arr * * @return array */ function divide(array $arr) { if (1 === count($arr)) { return $arr; } $left = $right = []; $middle = round(count($arr) / 2); for ($i = 0; $i < $middle; ++$i) { $left[] = $arr[$i]; } for ($i = $middle; $i < count($arr); ++$i) { $right[] = $arr[$i]; } $left = divide($left); $right = divide($right); return conquer($left, $right); }
function mathOperation($arg1, $arg2, $operation) { switch ($operation) { case 'add': echo adding($arg1, $arg2); break; case 'substract': echo substracting($arg1, $arg2); break; case 'multiply': echo multiply($arg1, $arg2); break; case 'divide': echo divide($arg1, $arg2); break; default: echo "Вы не корректно определили параметры!"; break; } }
} function divide($a, $b) { if ($b == 0) { return "Error: You cannot divide by 0\n"; } elseif (is_numeric($a) && is_numeric($b)) { return $a / $b; } else { return errorMessage($a, $b); } } function modulus($a, $b) { if (b == 0) { return "Error: You cannot divide by 0\n"; } elseif (is_numeric($a) && is_numeric($b)) { return $a % $b; } else { return errorMessage($a, $b); } } function errorMessage($a, $b) { return 'ERROR: Please input only numbers for $a is ' . $a . ' and $b is ' . $b . PHP_EOL; } // Add code to test your functions here echo add($a, $b) . PHP_EOL; echo subtract($a, $b) . PHP_EOL; echo multiply($a, $b) . PHP_EOL; echo divide($a, 0) . PHP_EOL; echo modulus($a, $b) . PHP_EOL;
echo 'OK<br>'; } else { echo 'erro<br>'; } if (multi(5, 2) == 5 * 2) { echo 'OK<br>'; } else { echo 'erro<br>'; } if (divide(5, 2) == 5 / 2) { echo 'OK<br>'; } else { echo 'erro<br>'; } if (some(5, multi(2, 3)) == 5 + 2 * 3) { echo 'OK<br>'; } else { echo 'erro<br>'; } if (subtrair(8, some(3, 5)) == 8 - (3 + 5)) { echo 'OK<br>'; } else { echo 'erro<br>'; } if (some(divide(9, 3), multi(4, 2)) == 9 / 3 + 4 * 2) { echo 'OK<br>'; } else { echo 'erro<br>'; } ?> </pre>
public function use(...$fonts) { $eol = EOL; $str = "<style type='text/css'>" . $eol; $args = $this->_parameters($fonts, 'fonts'); $lastParam = $args->lastParam; $arguments = $args->arguments; $links = $args->cdnLinks; foreach ($arguments as $font) { if (is_array($font)) { $font = ''; } $f = divide($font, "/", -1); // SVG IE VE MOZILLA DESTEKLEMIYOR $fontFile = FONTS_DIR . $font; if (!is_file($fontFile)) { $fontFile = EXTERNAL_FONTS_DIR . $font; } $baseUrl = baseUrl($fontFile); if (extension($fontFile)) { if (is_file($fontFile)) { $strEx = Import::something($fontFile, '', true); break; } } if (is_file($fontFile . ".svg")) { $str .= '@font-face{font-family:"' . $f . '"; src:url("' . $baseUrl . '.svg") format("truetype")}' . $eol; } if (is_file($fontFile . ".woff")) { $str .= '@font-face{font-family:"' . $f . '"; src:url("' . $baseUrl . '.woff") format("truetype")}' . $eol; } // OTF IE VE CHROME DESTEKLEMIYOR if (is_file($fontFile . ".otf")) { $str .= '@font-face{font-family:"' . $f . '"; src:url("' . $baseUrl . '.otf") format("truetype")}' . $eol; } // TTF IE DESTEKLEMIYOR if (is_file($fontFile . ".ttf")) { $str .= '@font-face{font-family:"' . $f . '"; src:url("' . $baseUrl . '.ttf") format("truetype")}' . $eol; } // CND ENTEGRASYON $cndFont = isset($links[strtolower($font)]) ? $links[strtolower($font)] : NULL; if (!empty($cndFont)) { $str .= '@font-face{font-family:"' . divide(removeExtension($cndFont), "/", -1) . '"; src:url("' . $cndFont . '") format("truetype")}' . $eol; } // FARKLI FONTLAR $differentSet = Config::get('ViewObjects', 'font')['differentFontExtensions']; if (!empty($differentSet)) { foreach ($differentSet as $of) { if (is_file($fontFile . prefix($of, '.'))) { $str .= '@font-face{font-family:"' . $f . '"; src:url("' . $baseUrl . prefix($of, '.') . '") format("truetype")}' . $eol; } } } // EOT IE DESTEKLIYOR if (is_file($fontFile . ".eot")) { $str .= '<!--[if IE]>'; $str .= '@font-face{font-family:"' . $f . '"; src:url("' . $baseUrl . '.eot") format("truetype")}'; $str .= '<![endif]-->'; $str .= $eol; } } $str .= '</style>' . $eol; if (isset($strEx)) { $str = $strEx; } if (!empty($str)) { if ($lastParam === true) { return $str; } else { echo $str; } } else { return false; } }
<?php error_reporting(E_ALL); // set error reporting on function divide($numerator, $denominator) { return $numerator / $denominator; } echo divide(1, 2) . '<br />' . PHP_EOL; // 0.5 echo divide(1, 0) . '<br />' . PHP_EOL; // "Warning: Division by zero" echo @divide(1, 0) . '<br />' . PHP_EOL; // (suppressed) echo divide(1, 2) . '<br />' . PHP_EOL; // 0.5
public function __call($method, $param) { die(getErrorMessage('Error', 'undefinedFunction', divide(str_ireplace(INTERNAL_ACCESS, '', get_called_class()), '\\', -1) . "::{$method}()")); }
} else { echo "ERROR: Cannot divide by 0\n"; } } } function modulus($a, $b) { $error = errorCheck($a, $b); if ($error) { if ($a != 0 && $b != 0) { return $a % $b; } else { echo "ERROR: Cannot divide by 0\n"; } } } function errorCheck($a, $b) { if (!is_numeric($a) || !is_numeric($b)) { echo "ERROR: Both arguments must be numbers\n"; return false; } else { return true; } } // Add code to test your functions here echo add(10, 2) . PHP_EOL; echo subtract(10, 2) . PHP_EOL; echo multiply(10, 2) . PHP_EOL; echo divide(10, 2) . PHP_EOL; echo modulus(10, 2) . PHP_EOL;
<?php function add($a, $b) { return $a + $b; } function subtract($a, $b) { return $a - $b; } function multiply($a, $b) { $tot = 0; for ($i = 0; $i < $a; $i++) { $tot += $b; } return $tot; } function divide($a, $b) { return $a / $b; } echo "add: " . add(6, 4) . PHP_EOL; echo "sub: " . subtract(12, 2) . PHP_EOL; echo "mul: " . multiply(5, 2) . PHP_EOL; echo "div: " . divide(20, 2) . PHP_EOL;
} } function modulus($a, $b) { $isValid = validate($a, $b); if ($b == 0) { return "Error: We can't modulus by zero."; } if ($isValid === true) { return $a % $b; } else { return $isValid; } } function multbyadd($a, $b) { $sum = 0; for ($i = 0; $i < $b; $i++) { $sum = $sum + $a; } return $sum; // above works. or i=1; i<=b. } echo add("a", 7) . PHP_EOL; echo subtract("a", 7) . PHP_EOL; echo multiply("a", 7) . PHP_EOL; echo multiply(2, 7) . PHP_EOL; echo divide(2, 0) . PHP_EOL; echo divide(2, 7) . PHP_EOL; echo modulus(2, 7) . PHP_EOL; echo multbyadd(2, 7) . PHP_EOL;
if (isset($func)) { switch ($func) { case "add": $response = add($calculate); return json_encode($response); break; case "subtract": $response = subtract($calculate); return json_encode($response); break; case "multiply": $response = multiply($calculate); return json_encode($response); break; case "divide": $response = divide($calculate); return json_encode($response); break; } } function add($calculate) { $addItems = $calculate; $piece = explode(" ", $addItems); foreach ($piece as $pieces) { if ($pieces == "plus") { continue; } $equation = $piece[0] + $piece[2]; } $finalequation = "" . $equation . "";
<?php $a = 0; $b = 42 / $a; function divide($a) { if ($a == 0) { throw new Exception("Division by zero!"); } return 42 / $a; } echo divide(2) . "\n"; //echo divide(0) . "\n"; try { echo divide(2) . "\n"; echo divide(0) . "\n"; } catch (Exception $exception) { echo $exception->getMessage(); }
function subtract($a, $b, $order = true) { return ($order ? $a - $b : $b - $a) . PHP_EOL; } function multiply($a, $b) { return $a * $b . PHP_EOL; } function divide($a, $b, $order = true) { return ($order ? $a / $b : $b / $a) . PHP_EOL; } echo subtract(10, 5, false); echo add(10, 5); echo multiply(10, 5); echo divide(10, 5, false); ?> </section> <section id="exercise6"> <h3>Exercise 6</h3> <p>Create a function which can accept any number of integer arguments and which returns their sum.</p> <?php function sum() { $sum = 0; if (func_num_args() > 0) { //check number of args supplied foreach (func_get_args() as $arg) { if (is_numeric($arg)) {
protected function _argumentPassed($msg, $file, $line, $trace) { $exceptionData = false; preg_match('/^Argument\\s(\\d)+\\spassed\\sto\\s(.*?)::(.*?)\\smust\\sbe\\s\\w+\\s\\w+\\s\\w+\\s(.*?),\\s(\\w+)\\sgiven/xi', $msg, $match); $message = !empty($match[0]) ? $match[0] : NULL; $argument = !empty($match[1]) ? $match[1] : NULL; $class = !empty($match[2]) ? $match[2] : NULL; $method = !empty($match[3]) ? $match[3] : NULL; $type = !empty($match[4]) ? strtolower(divide($match[4], '\\', -1)) : NULL; $data = !empty($match[5]) ? strtolower($match[5]) : NULL; if (empty($match)) { return false; } if (!empty($trace)) { $traceInfo = $this->_traceFinder($trace, 2, 0); } else { $traceInfo = $this->_traceFinder(debug_backtrace(2), 7, 5); } if ($type !== $data) { $langMessage1 = '[' . $this->_cleanClassName($class) . '::' . $method . '] p' . $argument . ':'; $langMessage2 = '[`' . $type . '`]'; $exceptionData = ['message' => lang('Error', 'typeHint', ['&' => $langMessage1, '%' => $langMessage2]), 'file' => $traceInfo['file'], 'line' => '[' . $traceInfo['line'] . ']']; return $exceptionData; } return true; }
{ if ($b == 0) { return 'ERROR: You are trying to destroy us all! ' . $a . ' ' . $b; } // $isValid = errorMessage($a, $b); if (validate($a, $b)) { return $a / $b; } // } else { // return $isValid; // } } function modulus($a, $b) { if ($b == 0) { return 'ERROR: You are trying to destroy us all! ' . $a . ' ' . $b; } // $isValid = errorMessage($a, $b); if (validate($a, $b)) { return $a % $b; } // } else { // return $isValid; // } } echo add(9, "notNumeric") . PHP_EOL; echo subtract(9, 3) . PHP_EOL; echo multiply(9, 3) . PHP_EOL; echo divide(9, 0) . PHP_EOL; echo modulus(9, 3) . PHP_EOL; // Add code to test your functions here
public function something($randomPageVariable = '', $randomDataVariable = '', $randomObGetContentsVariable = false) { if (!empty($this->parameters['usable'])) { $randomObGetContentsVariable = $this->parameters['usable']; } if (!empty($this->parameters['data'])) { $randomDataVariable = $this->parameters['data']; } $this->parameters = array(); $eol = eol(); $randomPageVariableExtension = extension($randomPageVariable); $randomPageVariableBaseUrl = baseUrl($randomPageVariable); $return = ''; if (!is_file($randomPageVariable)) { return Error::set('Error', 'fileParameter', '1.(randomPageVariable)'); } $randomPageVariable = restorationPath($randomPageVariable); if ($randomPageVariableExtension === 'js') { $return = '<script type="text/javascript" src="' . $randomPageVariableBaseUrl . '"></script>' . $eol; } elseif ($randomPageVariableExtension === 'css') { $return = '<link href="' . $randomPageVariableBaseUrl . '" rel="stylesheet" type="text/css" />' . $eol; } elseif (stristr('svg|woff|otf|ttf|' . implode('|', Config::get('Font', 'differentFontExtensions')), $randomPageVariableExtension)) { $return = '<style type="text/css">@font-face{font-family:"' . divide(removeExtension($randomPageVariable), "/", -1) . '"; src:url("' . $randomPageVariableBaseUrl . '") format("truetype")}</style>' . $eol; } elseif ($randomPageVariableExtension === 'eot') { $return = '<style type="text/css"><!--[if IE]>@font-face{font-family:"' . divide(removeExtension($randomPageVariable), "/", -1) . '"; src:url("' . $randomPageVariableBaseUrl . '") format("truetype")}<![endif]--></style>' . $eol; } else { $randomPageVariable = suffix($randomPageVariable, '.php'); if (is_file($randomPageVariable)) { if (is_array($randomDataVariable)) { extract($randomDataVariable, EXTR_OVERWRITE, 'zn'); } if ($randomObGetContentsVariable === false) { require $randomPageVariable; } else { ob_start(); require $randomPageVariable; $randomContentVariable = ob_get_contents(); ob_end_clean(); return $randomContentVariable; } } } if ($randomObGetContentsVariable === false) { echo $return; } else { return $return; } }
return $answer; } $result = 0; $result = multiply(2, 5); var_dump($result); $result = 0; $result = divide(25, 5); var_dump($result); $multiply_result = $divide_result = 0; if (isset($_GET["operator"])) { echo $_GET["operator"]; if ($_GET["operator"] == "multiply") { $multiply_result = multiply($_GET["number_one"], $_GET["number_two"]); } if ($_GET["operator"] == "divide") { $divide_result = divide($_GET["number_one"], $_GET["number_two"]); } } ?> <h1>Korruta</h1> <form> <input type="hidden" name="operator" value="multiply"> <input name="number_one"> x <input name="number_two"> = <?php echo $multiply_result; ?> <input type="submit"> </form> <h1>Jaga</h1> <form>
// calc.php function multiply($x, $y) { $answer = $x * $y; return $answer; } function divide($x, $y) { $answer = $x / $y; return $answer; } $result = 0; $result = multiply(2, 5); var_dump($result); $result = divide(10, 3); var_dump($result); $multiply_result = $divide_result = 0; if (isset($_GET["operator"])) { echo $_GET["operator"]; //kontrollin millega operator võrdub // == "multiply" siis korrutan arvud > multiply(1,2) if ($_GET["operator"] == "multiply") { //aadressirealt 2 arvu ja saatsin funktsiooni $multiply_result = multiply($_GET["number_one"], $_GET["number_two"]); } } ?> <h1> Korruta </h1> <form>
public function onlyName(string $class) : string { return divide(str_replace(INTERNAL_ACCESS, '', $class), '\\', -1); }
} moveforward(); moveforward(); turn("Right"); moveforward(); $ans = add(3, 4); echo $ans; echo "<br>"; // challenge create a function that returns a / b function divide($a, $b) { return $a / $b; } function getbignumber() { return 5; } $result = divide(3, 3); echo $result; $ans = add(3, getbignumber()); echo $ans; echo strlen("Billy"); if (is_numeric("hello")) { echo "Yep"; } else { echo "Nope"; } echo ucwords("so much depends upon the red wheel barrow"); //ucfirst, is_numeric,ucwords,substr,strlen,str_replace echo substr("This is rad", 0, 2); echo str_replace("e", "!", "here is a sentance with vowels and verbs subject");
<?php function add($number1, $number2) { $result = $number1 + $number2; return $result; } echo add(10, 10) + 100; echo '<br/>'; function divide($number1, $number2) { $result = $number1 / $number2; return $result; } $sum = divide(add(10, 10), add(5, 5)); echo $sum;
<?php function divide($num1, $num2) { if ($num2 == 0) { throw new Exception('cannot divide by zero'); } else { return $num1 / $num2; } } echo divide(10, 0);
*/ $stack = preg_split('/ *([+\\-\\/*]) */', $question, -1, PREG_SPLIT_DELIM_CAPTURE); //The stack array must have a minimum of 3 elements to form a valid math operation: [A] [OPERATOR] [B] while (sizeof($stack) >= 3) { switch ($stack[1]) { case "+": $answer = addition($stack[0], $stack[2]); break; case "-": $answer = subtract($stack[0], $stack[2]); break; case "*": $answer = multiply($stack[0], $stack[2]); break; case "/": $answer = divide($stack[0], $stack[2]); break; } //Remove the first 3 elements from the stack as they have been processed $stack = array_slice($stack, 3); //Add the current answer total to the beginning of the array for any further processes array_unshift($stack, $answer); } function addition($val1, $val2) { return $val1 + $val2; } function subtract($val1, $val2) { return $val1 - $val2; }