1 PHP: Arrays, Functions and Form processing Overview This lab walks you through using PHP to create simple applications. PHP is popular for many Web applications, so becoming comfortable with the...

i will send you the Ubuntu Virtual machine iso through gmail. all u need to do is to upload this iso file into ur virtual box and power it on. that's it. everything has already been configured in this ISO. Some of the exercise will be easier if done in this provided virtual machine. email me if u have any quetion with downloading of this ISO.

Lab4 due 02/03/2018
Lab5 due 02/08/2018
Lab6 due 02/15/2018
lab7 due 02/22/2018
lab8 due 02/28/04



1 PHP: Arrays, Functions and Form processing Overview This lab walks you through using PHP to create simple applications. PHP is popular for many Web applications, so becoming comfortable with the syntax of PHP will help you diagnose and identify potential security issues. It is not envisioned you will become an expert in PHP from this course, but you will be able to create simple Web applications; and, in the near future of this course, analyze that code for security issues. Learning Outcomes: At the completion of the lab you should be able to: 1. Create and test PHP scripts that include Arrays 2. Create and test PHP scripts that include Functions 3. Create, and test PHP scripts to process HTML Forms 4. Compare and contrast session creating mechanisms in PHP Lab Submission Requirements: After completing this lab, you will submit a word (or PDF) document that meets all of the requirements in the description at the end of this document. In addition, your Web Application files should be submitted. You can submit multiple files in a zip file. Virtual Machine Account Information Your Virtual Machine has been preconfigured with all of the software you will need for this class. The default username and password are: Username : umucsdev Password: umuc$d8v Part 1 – Create and test PHP scripts that include Arrays This exercise will walk through creating a PHP script that creates, uses and manipulates arrays. We will use the gedit text editor to create the PHP file in the Virtual Machine. 1. After launching the gedit text editor, create a new document and type or copy and paste the PHP code shown below: Arrays Demo 2

PHP Arrays Demo


Current Numbers "; // Create a table and display the numbers echo ""; foreach ( $numbers as $val ) { echo ""; echo ""; echo ""; } echo "





$val
"; // Sort the array sort($numbers); echo "


Sorted Numbers

"; // Create a table and display the numbers echo ""; foreach ( $numbers as $val ) { echo ""; echo ""; echo ""; } echo "





$val
"; // Create an Associate array // Multi-dimensional array $gpa=array( array( "student"=>"Joe Smith", "grade" =>"A" ), array( "student"=>"Mary Jones", "grade" =>"A" ), array( "student"=>"John Perry", "grade" =>"C" ), ); // Display the Student Data echo "


Student data

"; echo ""; echo ""; 3 // Loop through each dimension of the array foreach ( $gpa as $g ) { echo ""; foreach ( $g as $value ) { echo ""; } echo ""; } echo ""; echo "









Student Name

Current Grade
$value
"; // Sort the Associative Array sort($gpa); // Display the Sorted Student Data echo "


Sorted Student data

"; echo ""; echo ""; // Loop through each dimension of the array foreach ( $gpa as $g ) { echo ""; foreach ( $g as $value ) { echo ""; } echo ""; } echo ""; echo "









Student Name

Current Grade
$value
"; ?> Save the file in the /var/www/html/week4 folder in a file named ArraysDemo.php. Note, you may need to create a folder named week4. Recall the /var/www/html is the location of the Apache2 web server html files. Creating separate folders for each week or application will help organize the server. 4 Launch the Firefox browser and run your home page by entering the following URL: localhost/week4/ArraysDemo.php 5 As you analyze and experiment with the code, you should note the integration of the Arrays into the html displays. Also, note how the arrays are declared and initialized with data: For a single dimensional array the declaration and initialization is fairly straight forward: $numbers = array( 11,43,4,5,7,10); For a multi-dimensional associative array the syntax is trickier: $gpa=array( array( "student"=>"Joe Smith", "grade" =>"A" ), array( "student"=>"Mary Jones", "grade" =>"A" ), array( "student"=>"John Perry", "grade" =>"C" ), ); Notice the use of a nested array statements and use of => to associated a value for array element. 6 2. As before, you can also run the PHP code directly from the shell prompt. To run from the shell prompt, open a shell prompt, change to the location of the ArraysDemo.php file and type: php ArraysDemo.php Running from the shell may provide some insight when you php Errors that prevent the cause the script to stop running prior to producing the HTML output. Part 2 Create and test PHP scripts that include Functions In this exercise we will create a PHP web page that uses both existing and user-defined PHP functions. Functions are used to help organize code into sub-units to allow for code reuse and reproducible results. 1. Copy and paste the following code into a file named FunctionsDemo.php in the /var/www/html/week4 folder on your Virtual Machine. Functions Demo

PHP Functions Demo


Example PHP Functions "; // Create a table and display the numbers echo ""; echo ""; foreach ( $numbers as $val ) { echo ""; echo ""; echo ""; echo ""; echo ""; 7 echo ""; echo ""; echo ""; } echo "


















Degree

Sqrt(Degree)

sin(Degree)

cos(Degree)

tan(Degree)

cubeIt(Degree)
" . $val . "" . sqrt($val). "" . sin(deg2rad($val)). "" . cos(deg2rad($val)). "" . tan(deg2rad($val)). "" . cubeIt($val). "
"; // Simple Cube function // Return the cube of the input value function cubeIt($val) { return $val*$val*$val; } ?> 2. Launch your Firefox browser and run the Web application. Assuming you placed the file in the /var/www/html/week4 folder you can run this by typing the following URL on your Virtual machine: localhost/week4/FunctionsDemo.php. If successful, the resulting output will look similar to this: 3. Reviewing the code you should note the following: 8 a. Existing PHP functions can be used easily by calling the function name and any required parameters. For this example, sqrt(), deg2rad(), sin(), cos() and tan() existing functions were called. b. PHP functions you create should be of the format: function functionName($parameter1, $parameter2 …) { // Code here return $returnvalue; } c. You can create functions with any level of rigor and complexity as needed to solve the computing problem at hand. The simple PHP function provided for this example calculates the cube of the input parameter: function cubeIt($val) { return $val*$val*$val; } Part 3 Create and test PHP scripts to process HTML Forms In this exercise we will create a PHP web pages that include simple forms that use get and post methods
Jan 31, 2020Swinburne University of Technology
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here