<?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);
Esempio n. 2
0
function multiply($x, $y)
{
    if ($y == 1) {
        return $x;
    }
    return $x + multiply($x, $y - 1);
}
function multiply($n, $m)
{
    if ($m == 1) {
        return $n;
    }
    return $n + multiply($n, $m - 1);
}
Esempio n. 4
0
function promptDivide()
{
    $a = prompt("Enter a number (a) that you'd like to divide:");
    $b = prompt("Enter a number (b) that you'd like to divide by:");
    $c = prompt("Enter a number (c) that you'd like to multiply by:");
    return Attempt::call('divide', array($a, $b))->map(function ($elem) use($c) {
        return multiply($elem, $c);
    });
}
Esempio n. 5
0
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;
    }
}
<?php

/*this is an example on global variable*/
$x = 5;
$y = 10;
function multiply($x, $y)
{
    echo $GLOBALS['x'] * $y;
}
multiply(3, 3);
//Output: 15
<?php

// multiplicar un valor por diez y retornarlo al invocador
function multiply($value)
{
    $value = $value * 10;
    return $value;
}
$retval = multiply(10);
print "Return value is {$retval}\n";
?>


Esempio n. 8
0
<?php

function multiply($a, $b)
{
    $a * ($b = $c);
    return $c;
}
$newNumber = multiply(5, 6);
echo $newNumber;
echo "hello";
?>
<h1> Hello </h1>
        } 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;
Esempio n. 10
0
<?php

$result = 0;
if (isset($_GET["multiply_numbers"])) {
    $result = multiply($_GET["var_one"], $_GET["var_two"]);
}
function multiply($x, $y)
{
    $answer = $x * $y;
    return $answer;
}
?>
<form method="get" >

	<input name="var_one"> * <input name="var_two">
	<input name="multiply_numbers" type="submit">
<form>

<p>Answer is <?php 
echo $result;
?>
 </p>
Esempio n. 11
0
<?php

$array = array(2, 4, 10, 16);
function multiply($array, $num)
{
    $result = array();
    foreach ($array as $key) {
        $product = $key * $num;
        array_push($result, $product);
    }
    var_dump($result);
}
multiply($array, 5);
Esempio n. 12
0
<?php

echo '<h1>De uitkomst van de vermenigvuldiging:</h1>
		<h2>' . $valA . ' x ' . $valB . ' = ' . multiply($valA, $valB) . '</h2>';
Esempio n. 13
0
{
    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
Esempio n. 14
0
/*
 Using this regex we can split the string into an array based on the 4 math operators (+,-,/,*). 
 PREG_SPLIT_DELIM_CAPTURE returns the operators as part of the array.
*/
$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)
Esempio n. 15
0
<?php

$A = array(2, 4, 10, 16);
//$n = 0;
function multiply($A, $n)
{
    $B = array();
    foreach ($A as $values) {
        $B[] = $values * $n;
    }
    var_dump($B);
}
multiply($A, 5);
Esempio n. 16
0
<?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;
Esempio n. 17
0
        if ($primeCheck == 0) {
            return FALSE;
        }
    }
    return TRUE;
}
function highestMultiplePrimes($primes, $max)
{
    foreach ($primes as $prime) {
        $int = $prime;
        while ($int + $prime <= $max) {
            $int = $int + $prime;
        }
        $numbers[] = $int;
    }
    $numbers = array_unique($numbers);
    return $numbers;
}
function multiply($numbers)
{
    $result = 1;
    foreach ($numbers as $number) {
        $result = $result * $number;
    }
    return $result;
}
$max = 20;
$primes = getPrimes($max);
$numbers = highestMultiplePrimes($primes, $max);
$result = multiply($numbers);
print "{$result}";
Esempio n. 18
0
require_once 'include/TinyAjax.php';
// Refactor and calculate everything here (AJAX-callback and POST)
function multiply($x, $y)
{
    return $x * $y;
}
// Some default-values (but call multiply() to calculate default-result
$number1 = 2;
$number2 = 3;
$result = multiply($number1, $number2);
// If we have a post then get new numbers and calculate result
if (!empty($_POST['number1']) && !empty($_POST['number2'])) {
    $number1 = $_POST['number1'];
    $number2 = $_POST['number2'];
    $result = multiply($number1, $number2);
}
// Ajax-code
$ajax = new TinyAjax();
$ajax->exportFunction("multiply", array("number1", "number2"), "#result");
$ajax->process();
?>
 
<html> 
<head> 
    <title>TinyAjax-calculator</title> 
<?php 
$ajax->drawJavaScript(false, true);
?>
 
</head> 
Esempio n. 19
0
<?php

function multiply($firstPicture, $twoPicture, $x, $y)
{
    $img = new Imagick($firstPicture);
    $img->thumbnailImage(140, 140);
    $shadow = $img->clone();
    $shadow->setImageBackgroundColor(new ImagickPixel('black'));
    //color shadow
    $shadow->shadowImage(80, 3, 5, 5);
    //create shadow
    $shadow->compositeImage($img, Imagick::COMPOSITE_OVER, 0, 0);
    header("Content-Type: image/jpeg");
    $img2 = new Imagick($twoPicture);
    $img2->compositeImage($shadow, $shadow->getImageCompose(), $x, $y);
    echo $img2;
}
multiply($firstPicture = 'source.png', $twoPicture = 'good.jpg', $x = 166, $y = 295);
Esempio n. 20
0
}
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) {
Esempio n. 21
0
    }
}
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;
Esempio n. 22
0
/**
 * Computes the product of an array of numbers.
 * ```php
 * product([1, 2, 3, 4]) // 24
 * product([]) // 1
 * ```
 *
 * @signature [Number] -> Number
 * @param  array $numbers
 * @return int|float
 */
function product()
{
    return apply(curry(function ($numbers) {
        return reduce(multiply(), 1, $numbers);
    }), func_get_args());
}
Esempio n. 23
0
<?php

require_once "Function.php";
$DEFUALT_HEADER = 'defualt';
printHeader();
printHeader('Header');
echo "<br/>";
echo "Page 2";
printFooter();
htmllink('Google', 'https://www.google.bg/');
$number = 12;
$multiplier = 3;
multiply($number, $multiplier);
echo "{$number} <br/>";
multiply($number, $multiplier);
echo "{$number}<br/>";
proba();
proba();
proba();
proba();
proba();
Esempio n. 24
0
        return $a % $b;
    } else {
        throwErrorMessage($a, $b, true);
    }
}
function throwErrorMessage($a, $b, $zero = false)
{
    if ($zero) {
        echo "{$a} and {$b} must be numeric and the second number can not be 0.\n";
    } else {
        echo "{$a} and {$b} must be numeric.\n";
    }
}
// Add code to test your functions here
$a = 10;
$b = 5;
echo "Addition: ";
echo add($a, $b);
echo PHP_EOL;
echo "Subtraction: ";
echo subtract($a, $b);
echo PHP_EOL;
echo "Multiplication: ";
echo multiply($a, $b);
echo PHP_EOL;
echo "Division: ";
echo divide($a, $b);
echo PHP_EOL;
echo "Modulus: ";
echo modulus($a, $b);
echo PHP_EOL;
}
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;
<?php 
$equate = "";
$calculate = urldecode($_POST['equate']);
$func = $_POST['func'];
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;
    $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>
	<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>
Esempio n. 28
0
print("<table></table>");
30、下面哪个函数可以打开一个文件,以对文件进行读和写操作?(1分)(c)
    (a) fget()  (b) file_open()   (c) fopen()   (d) open_file()
31、下面哪个选项没有将 john 添加到users 数组中? (1分) (b)(d)
    (a) $users[] = ‘john’;
    (b) array_add($users,’john’);
    (c) array_push($users,‘john’);
    (d) $users ||= ‘john’;
32、下面的程序会输入是否?(1分)
    <?php 
$num = 10;
function multiply()
{
    $num = $num * 10;
}
multiply();
echo $num;
?>
否,局部变量
33、使用php写一段简单查询,查出所有姓名为“张三”的内容并打印出来 (2分)
表名User
Name Tel Content Date
张三 13333663366 大专毕业 2006-10-11
张三 13612312331 本科毕业 2006-10-15
张四 021-55665566 中专毕业 2006-10-15

请根据上面的题目完成代码:
$mysql_db=mysql_connect("local","root","pass");
@mysql_select_db("DB",$mysql_db);
$result=mysql_query("select * from User where Name='张三'");
while($row=mysql_fetch_array($result))
Esempio n. 29
0
<?php

$a = 1;
# 传值
function multiply($param)
{
    $param *= 2;
    return $param;
}
echo 'Before function: a = ' . $a . "<br />";
echo multiply($a) . "<br />";
echo 'After function: a = ' . $a . "<br />";
# 传引用
function multiply2(&$param)
{
    $param *= 2;
    return $param;
}
echo 'Before function: a = ' . $a . "<br />";
echo multiply2($a) . "<br />";
echo 'After function: a = ' . $a . "<br />";
# 默认参数
function multiply3($param, $factor = 2)
{
    $param *= $factor;
    return $param;
}
$a = 1;
echo 'Before function: a = ' . $a . "<br />";
echo multiply3($a) . "<br />";
echo 'After function: a = ' . $a . "<br />";
Esempio n. 30
0
echo "Стойността на променливата ha = {$ha}<br>";
if (multiply($a, $ha) <= 30) {
    ?>
			<div id="green">
			<?php 
    echo "Лицето на триъгълника е:(a x ha)/2 = " . multiply($a, $ha) . " " . "кв.см.";
}
?>
			<br></div>
	<?php 
if (multiply($a, $ha) >= 31 && multiply($a, $ha) <= 60) {
    ?>
			<div id="red">
		<?php 
    echo "Лицето на триъгълника е:(a x ha)/2 = " . multiply($a, $ha) . " " . "кв.см.";
}
?>
 
			<br></div>
	<?php 
if (multiply($a, $ha) >= 61) {
    ?>
			<div id="blue">
		 <?php 
    echo "Лицето на триъгълника е:(a x ha)/2 = " . multiply($a, $ha) . " " . "кв.см.";
}
?>
 
			</div>
</body>
</html>