• Home
  • Swinburne University of Technology
  • Computing Technology Inquiry Project
  • Functions and Data Handling in PHP

Functions and Data Handling in PHP

COS10026 - Computing Technology Inquiry Project Facilitator Meeting Instructions 7 - Functions and Passing Data to Server Aims: · To be able to use various control structures and develop your own functions. · Use PHP predefined 'superglobal' variables to get data from a form. · Gain some of the knowledge and skills needed to complete Assignments. The tasks are due next week by your facilitator meeting. Email the link of your web page running on the Mercury server to your facilitator before the due date to be marked off. Tasks will not be marked if the email is not received. Task 1: Functions In this task we will apply · User-defined functions . In-built functions · if selection statements and loops Step 1: Creating functions Unzip the file lab07.zip into lab07 folder. Open the file mathfunctions.php in a text editor and read through the file. The user-defined function factorial accepts a positive integer and returns its factorial value. A factorial of a non- negative integer n, denoted by n! is the product of all positive integers less than or equal to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120 Add a function isPositiveInteger to mathfunctions.php as set out below. This user-defined function will use the in- built functions is_numeric() and floor() to check if a variable is an integer. function isPositiveInteger ($n) { // declare the function that takes a parameter $result = false; if (is numeric ($n) ) //use the inbuilt function is_numeric which returns boolean if ($n == floor ($n) ) if ($n >0 ) $result = true; return $result; // function execution will end here if -ve or non-integer - Aside: Why aren't braces needed after the if condition if (is_numeric($n))? While in this example braces are not used, it is good practice to always use them, as it can make the code easier to read and edit. Step 2: Call the functions Create an HTML/PHP file factorial.php that will include the file mathfunctions.php in order to access the defined functions in the file. The code below needs to be embedded in the HTML <body> of factorial.php. Page 1 COS10026 - Computing Technology Inquiry Project <body> <? php You will need to write the rest of the HTML around the <body> include ("mathfunctions . php") ; //makes functions in the included file available ?> <h1> Factorial </h1> <? php $num = 5; //enter some different values here to test - remove later if (isPositiveInteger ($num) ) { // call the function we defined in the previous step echo "<p>", $num, "! is ", factorial ($num), " .< /p>"; //ditto for factorial } else { // number is not an integer echo "<p>Please enter a positive integer .< /p>"; echo "<p><a href='factorial. html'>Return to the Entry Page</a></p>"; ?> </body> Note: At this stage, in the code above, the value of the variable $num = 5 is hard-coded for testing purposes. We will replace this with code to accept input from