function changes($selected_words) { list($uppercase, $symbol, $number) = checkBoxboolean(); if ($uppercase) { $selected_words = addUpperCase($selected_words); } if ($symbol) { $selected_words = addSymbols($selected_words); } if ($number) { $selected_words = addNumbers($selected_words); } return arrayConcatenateAndShuffle($selected_words); }
function displayArith($string) { $match = preg_match('/([\\d]+)([\\+\\*\\-])([\\d]+)/', $string, $matches); if (!$match) { throw new Exception("the expression is incorrect {$string}"); } $firstNumber = $matches[1]; $secondNumber = $matches[3]; $operator = $matches[2]; switch ($operator) { case '+': addNumbers($firstNumber, $secondNumber); break; case '-': subtractNumbers($firstNumber, $secondNumber); break; case '*': multiplyNumbers($firstNumber, $secondNumber); break; default: throw new Exception("undefined operator " . $operator); } }
<?php function hello() { print "<h1>HELLO!</h1>"; print "<p>Welcome to my web site</p>"; } function printBreak($text) { for ($x = 0; $x <= 10; $x++) { echo "The number is: {$x} <br>"; } print "{$text}<br>"; } function addNumbers($num1, $num2) { return $num1 + $num2; } hello(); printBreak("This is a line"); print addNumbers(3.75, 5.645);
<?php function addNumbers($number1, $number2) { if (is_numeric($number1) and is_numeric($number2)) { $result['answer'] = $number1 + $number2; $result['status'] = true; $result['message'] = "The answer is "; return $result; } else { $result['answer'] = null; $result['status'] = false; $result['message'] = "Unable to calculate. Non-numeric data."; return $result; } } $answer = addNumbers('2', 'all'); if ($answer['status']) { echo $answer['message'] . $answer['answer']; } else { echo $answer['message']; }
is_array : is_bool : is_double : is_int : is_null : is_numeric : is_string */ // empty() returns true or false if a var has a non-zero value echo 'Does $biggestNum exist '; echo empty($biggestNum) ? 'false' : 'true'; echo "</br></br>"; // isset() returns true or false if a variable exists echo 'Does $biggestNum exist '; echo isset($biggestNum) ? 'true' : 'false'; echo "</br></br>"; // You can execute unix commands by surrounding with `s echo `ls -la`; // Unix or OSX // echo `dir /w`; WINDOWS echo "</br></br>"; /* Functions allow you to reuse code A function must begin with a letter, but can contain numbers and underscores */ function addNumbers($num1, $num2) { return $num1 + $num2; } echo "3 + 4 = " . addNumbers(3, 4); ?> </body> </html>
<?php // This is known as the function "declaration" /** * Add two numbers together * @param int $num1 First number * @param int $num2 Second number * @return int This returns the sum of two numbers */ function addNumbers($num1, $num2) { return $num1 + $num2; } // Function usage $sum = addNumbers(21, 43); echo 'sum = ' . $sum;
* @param int $b * * @return int */ public function add($a, $b) { return $a + $b; } /** * @param int $a * @param int $b * * @return int */ public function multiply($a, $b) { return $a * $b; } } /** * @param int $a * @param int $b * * @return int */ function addNumbers($a, $b) { return $a + $b; } echo "98 + 3 = " . addNumbers(98, 3);
<?php /** * Using the require_once keyword we can include external files which contain functions we can then call. */ require_once "functions.php"; echo sayHello("Nick"); //=> Hello Nick echo addNumbers(10, 20); //=> 30
*/ doSomething(); function doSomething() { echo "This function has been invoked"; } function doSomething2() { echo "This function has been invoked"; } function addNumbers($number01, $number02) { $sum = $number01 + $number02; echo "<p> Sum is: {$sum}</p>"; } addNumbers(56, 325); echo "<p>Write and then invoke a function</p>"; function addNumbersWithReturn($number01, $number02) { $sum = $number01 + $number02; return $sum; } $result = addNumbersWithReturn(4, 234); echo "<h1>{$result}</h1>"; echo "<h1>" . addNumbersWithReturn(4, 234) . "</h1>"; if (isset($myName) == true) { echo "<p> Variable does exist</p>"; echo $myName; } else { echo "<p> Variable does NOT exist</p>"; }
<?php $tajuk = "Return Values Function"; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP | <?php echo $tajuk; ?> </title> </head> <body> <?php function addNumbers($number1, $number2) { $sum = $number1 + $number2; return $sum; } $result = addNumbers(34, 45); $result = addNumbers(100, $result); echo $result; ?> </body> </html>
<?php // make a function which makes an addition equation function addNumbers($num1, $num2) { return $num1 . " + " . $num2 . " = " . ($num1 + $num2); } // write out a few simple problems echo addNumbers(3, 5) . "<br>"; echo addNumbers(10, 20) . "<br>"; echo addNumbers(800, 200) . "<br>"; echo addNumbers(600, -200) . "<br>";
//create the array to hold the user dater and load $_GET into it $userArray = array(); foreach ($_GET as $key => $value) { $userArray[$key] = $value; } //Declare default values for variables $numberOfWords = 4; $separator = ""; //check that the variable keys exist in the userArray and load them if they do if (array_key_exists("numberOfWords", $userArray) && is_numeric($userArray["numberOfWords"]) && $userArray["numberOfWords"] > 0) { $numberOfWords = $userArray["numberOfWords"]; } if (array_key_exists("separator", $userArray)) { $separator = $userArray["separator"]; } //build the word portion of the password $passWords = selectWords($wordList, $numberOfWords, $separator); //if special characters are selected add them to the end if (array_key_exists("numSpecChar", $userArray) && is_numeric($userArray["numSpecChar"])) { $passWords = addSpecialChar($passWords, $userArray["numSpecChar"]); } //add a random number between 0 and 9 to the end of the password if (array_key_exists("numberInc", $userArray) && $userArray["numberInc"] > 0) { $passWords = addNumbers($passWords); } //chop the password down to the maximum length if required - cuts from the front if (array_key_exists("maximumLength", $userArray) && is_numeric($userArray["maximumLength"]) && $userArray["maximumLength"] > 0) { $passWords = maxLength($passWords, $userArray["maximumLength"]); } //display the password echo '<h4 class="passwordSection"> <br>' . $passWords . '</h4>';
<html> <head lang="en"> <meta charset="UTF-8"> <title>functions - Joris Bulters - MD2A</title> </head> <body> <?php include 'functions.php'; //See http://bovi.hosts.ma-cloud.nl/md2_bap/p2_w1_functions/ writeHello(); writeMsg('Hello person that reads this.'); writeMsg('Bye Bye mysterious person.'); writeMsgTo('Mysterious person', 'Welcome on this page.'); $outcome = addNumbers(5, 7); echo 'The outcome of 5 + 7 = ' . $outcome . '<br/>'; $outcome = calcRectNumb(5, 7); echo 'The area of a rectangle of 5 and 7 = ' . $outcome . '<br/>'; ?> <br> <hr> <h3>Currency Calculator</h3> <form action="money.php" method="post"> <p><input type="text" name="money" placeholder="Dollars -> Euros"></p> <p><input type="submit" value="Verstuur" name="go"></p> <p><input type="reset" value="Reset"></p> </form>
<?php function addNumbers($number1, $number2) { $result = $number1 + $number2; return $result; } $answer = addNumbers('2', '3'); echo $answer;
addSix($orignum); echo "Original Value is {$orignum}<br />"; ?> <!-- ==== reutn statement in Function === --> <br> <?php function addNumbers($num1, $num2) { $sum = $num1 + $num2; return $sum; } $return_value = addNumbers(10, 20); echo "Returned value from the function : {$return_value}"; ?> <br> <?php function addFunction2($num1, $num2, $num3) { $add = $num1 + $num2; $sub = $num3 - $num1; return array($add, $sub); } $result = addFunction2(10, 20, 30); echo " Result 1 = " . $result[0] . "<br>";