function mathOperation($arg1, $arg2, $operation) { switch ($operation) { case 'addition': echo "Результат сложения {$arg1} и {$arg2} равен " . addition($arg1, $arg2) . "<br>"; break; case 'subtraction': echo "Результат вычитания {$arg2} из {$arg1} равен " . subtraction($arg1, $arg2) . "<br>"; break; case 'multiplication': echo "Результат умножения {$arg1} на {$arg2} равен " . multiplication($arg1, $arg2) . "<br>"; break; case 'remainder of the division': echo "Результат вычисления остатка при делении {$arg1} на {$arg2} равен " . remainderOfTheDivision($arg1, $arg2) . "<br>"; break; default: echo "Введено неверное название операции<br>"; break; } }
</div> <br><br> <table width="500" border="0" align="center" cellpadding="1" cellspacing="1" class="tbl_border"> <tr> <td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="tbl_gray_wborder"> <tr> <td class="txt14b_black"> <p align="center"><span class="txt16b_blue">Sample Output: </span><br> <br> <?php function addition($val1, $val2) { $sum = $val1 + $val2; return $sum; } addition(3, 4); $new_val = addition(3, 4); if (addition(5, 6) == 11) { //echo "Yes"; } ?> <br> </p></td> </tr> </table></td> </tr> </table> </body> </html>
<?php echo "<h1>Welcome Samuel!</h1>"; //Usage of echo echo "<br>"; $age = array("Samuel" => "29", "Sudhakar" => "30"); //Associative array echo "Samuel is " . $age['Samuel'] . " years old.<br>"; $age1 = 29; echo "Samuel is {$age1} years old.<br>"; //Global variables test echo "Global variables test (Exception)"; $x = 75; $y = 25; function addition() { $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; } addition(); echo "<br>"; //echo $z; echo $y; echo $y; echo $x;
function say_hello() { echo "Hello World! <br />"; } say_hello(); function say_hello2($word) { echo "Hello {$word}! <br />"; } say_hello2("World"); say_hello2("Universe"); $name = "John Doe"; say_hello2($name); //$target is an internal variable that is only used within //the function function say_hello3($greeting, $target, $punct) { echo $greeting . ", " . $target . $punct . "<br />"; } say_hello3("Greetings", $name, "!"); ?> <br /> <?php function addition($val1, $val2) { $sum = $val1 + $val2; } echo addition(3, 4); ?> </body> </html>
echo "<h2 style='text-align:center;'>Sorry no, " . $var1 . " is equal to " . $var2 . ".</h2>"; } } //--Math System $question = $query[1]; $answer = 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 test() { $a = 23; global $a; //define global scope first echo $a . "</br>"; /* reference to local scope variable */ } test(); function addition($two, $one) { // static $two = 3; // static $one = 5; echo $two * $one . "</br>"; } addition(3, 15); /*---------------------------------- S U P E R G L O B A L S $GLOBALS $_SERVER $_GET $_POST $_SESSION Next we have a CLASS example with setter and getter for prop values ------------------------------------*/ /*---------------------------------- P R O P S C O P E S
echo $greeting . ", " . $target . $punct . "<br />"; return true; } say_hello3("Greetings", $name, "!"); ?> <br /> <?php function addition($val1, $val2) { $sum = $val1 + $val2; return $sum; } $new_val = addition(3, 4); echo $new_val; echo "<br />"; $new_val2 = addition(5, 6) == 11; echo "yes"; echo "<br />"; function add_subt($val1, $val2) { $add = $val1 + $val2; $subt = $val1 - $val2; $result = array($add, $subt); return $result; } $result_array = add_subt(10, 5); echo "Add: " . $result_array[0]; echo "<br />"; echo "Subt: " . $result_array[1]; ?> </body>
function sayName3($greeting, $target, $punct) { echo $greeting . ", " . $target . $punct . "!"; } sayName3("Good evening", $name, "!"); ?> <!-- returning a value from a function --> <br> <?php function addition($var1, $var2) { $sum = $var1 + $var2; return $sum; } // assigning the value to a variable $value = addition(1, 2); echo $value; ?> <!-- returning two values using an array --> <br> <?php function add_sub($var1, $var2) { $add = $var1 + $var2; $sub = $var1 - $var2; // <!-- inserting the values in an array --> $result = array($add, $sub); return $result; } $addsub_array = add_sub(10, 8); echo "Addition: " . $addsub_array[0] . "<br>";