Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.

Similar presentations


Presentation on theme: "Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy."— Presentation transcript:

1 Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy

2 Agenda Lab and homework review Lab and homework review Canvas Canvas Array Array Functions Functions PHP functions PHP functions Homework and presentation Homework and presentation

3 Canvas Goto https://learn.maricopa.edu/login Goto https://learn.maricopa.edu/loginhttps://learn.maricopa.edu/login Use MEID/Password to log-in Use MEID/Password to log-in

4 First Things First When you come to class, download Filezilla to the desktop and install it! When you come to class, download Filezilla to the desktop and install it! Make sure it is the client piece Make sure it is the client piece http://filezilla-project.org/

5 IMPORTANT NOTE All filenames lower case alphanumeric names.

6 Arrays An ordered arrangement of data elements. An ordered arrangement of data elements. Vector is a one dimensional array. Vector is a one dimensional array. Matrix is a two-dimensional array. Matrix is a two-dimensional array. http://www.w3schools.com/php/php_arrays.asp

7 Numeric Array Syntax Creating an array: Creating an array:$a=array(“Cat",“Dog",“Bird",“Fish"); Accessing elements of an array. Note: the first element is location “0”. Accessing elements of an array. Note: the first element is location “0”. print(“ Element = “.$a[0].” \n”); Result: “Element = Cat” Result: “Element = Cat”

8 Associative Array Syntax Creating an array: Creating an array:$a=array(“Cat“=>4,“Dog“=>2,“Bird“=>7,“Fish“=>20); Accessing elements of an array. Accessing elements of an array. print(“ Element = “.$a[“Bird”].” \n”); Result: “Element = 7” Result: “Element = 7”

9 Multi Dimension Array Creating an array: $simpsons = array( array(“Homer", 35, 315), array(“Marge", 33, 125), array(“Bart", 11, 95)); Creating an array: $simpsons = array( array(“Homer", 35, 315), array(“Marge", 33, 125), array(“Bart", 11, 95)); Accessing elements: echo $simpsons[0][1] ; // Homer’s age Accessing elements: echo $simpsons[0][1] ; // Homer’s age

10 Multi Dimensional Array (adv) Creating array: $simpsons = array( array(Name => “Homer", Age => 35, Weight => 315), array( Name => “Marge", Age => 33, Number => 125), array( Name => “Bart", Age => 11, Number => 95)); Creating array: $simpsons = array( array(Name => “Homer", Age => 35, Weight => 315), array( Name => “Marge", Age => 33, Number => 125), array( Name => “Bart", Age => 11, Number => 95)); Accessing elements: echo $simpsons[0][“Age”] ; // Homer’s age Accessing elements: echo $simpsons[0][“Age”] ; // Homer’s age

11 Functions Definition: A self-contained software routine that performs a task. Definition: A self-contained software routine that performs a task. Functions can do a large amount of processing or as little as adding two numbers and deriving a result. Functions can do a large amount of processing or as little as adding two numbers and deriving a result. Values may be passed to a function. Values may be passed to a function. Values may be returned from a function. Values may be returned from a function. http://www.w3schools.com/php/php_functions.asp

12 Function Syntax function addnumbers ($a, $b) { $c = $a + $b; return $c; } addnumbers = function name addnumbers = function name $a and $b = localized variables [Parameters] $a and $b = localized variables [Parameters] return = sends value out if function return = sends value out if function http://www.w3schools.com/php/php_functions.asp

13 Function Flow $a = “rob”; $b = introduce(“Tom”); // Function CALL function introduce ($a) // Function DEFINITION { return “Hello “. $a; // Function RETURN } echo $b; RESULTS: Hello Tom

14 PHP Built-in Functions PHP natively has hundreds of functions created for use. PHP natively has hundreds of functions created for use. Users CAN NOT create functions that have the same name as the PHP functions. Users CAN NOT create functions that have the same name as the PHP functions. Functions could vary by server as some are tied to modules of PHP installed. Functions could vary by server as some are tied to modules of PHP installed. The default PHP built-in functions are listed at http://php.net/quickref.php The default PHP built-in functions are listed at http://php.net/quickref.php http://php.net/quickref.php

15 PHP Array functions array_rand() Pick random entries out of an array array_rand() Pick random entries out of an array array_rand array_reverse() Return an array with elements in reverse order array_reverse() Return an array with elements in reverse order array_reverse array_sum() Calculate the sum of values in an array array_sum() Calculate the sum of values in an array array_sum http://php.net/manual/en/ref.array.php

16 IMPORTANT NOTE A variable in a function is NOT the same as a variable inside a function even if they have the SAME name. This is called SCOPE.

17 PHP.NET The official PHP information website. The official PHP information website. User community of comments and questions and solutions. User community of comments and questions and solutions. Good examples. Good examples. ZEND owns the management of PHP. ZEND owns the management of PHP.

18 PHP.NET Site Examples Warnings User Content Explanations

19 PHP.NET References http://www.php.net/manual/en/language.basic-syntax.php http://www.php.net/manual/en/language.basic-syntax.php http://www.php.net/manual/en/language.basic-syntax.php http://www.php.net/manual/en/language.variables.php http://www.php.net/manual/en/language.variables.php http://www.php.net/manual/en/language.variables.php http://www.php.net/manual/en/language.operators.php http://www.php.net/manual/en/language.operators.php http://www.php.net/manual/en/language.operators.php http://www.php.net/manual/en/language.control- structures.php http://www.php.net/manual/en/language.control- structures.php http://www.php.net/manual/en/language.control- structures.php http://www.php.net/manual/en/language.control- structures.php http://www.php.net/manual/en/language.functions.php http://www.php.net/manual/en/language.functions.php http://www.php.net/manual/en/language.functions.php

20 IMPORTANT NOTE The life cycle in PHP is the length of time it takes the server to find the file, process the information, and send the completed static HTML back to the client.

21 PHP Require/Include require() pulls content from another file into the current file require() pulls content from another file into the current file All PHP code will run as if it was entered in the page from the beginning All PHP code will run as if it was entered in the page from the beginning include() and require() do the SAME thing except if the require can not find the file it is looking for the page stops loading include() and require() do the SAME thing except if the require can not find the file it is looking for the page stops loading There is also include_once() and require_once() which enforces loading the external content only once There is also include_once() and require_once() which enforces loading the external content only once

22 Require and Include Syntax require(“functions.php”); require(“functions.php”); include(“functions.php”); include(“functions.php”); include_once(“functions.php”); include_once(“functions.php”); include(“library\functions.php”); include(“library\functions.php”); There needs to be a file called functions.php in the SAME folder as the original file. There needs to be a file called functions.php in the “library” folder below the original file.

23 Questions?

24 Homework Create a folder called “assignment1” Create a folder called “assignment1” Rewrite the calculator program to utilize functions for the math functions. Rewrite the calculator program to utilize functions for the math functions. Add a function to check every input for a negative number and display the text “YOU CAN NOT ENTER A NEGATIVE.” Add a function to check every input for a negative number and display the text “YOU CAN NOT ENTER A NEGATIVE.” Add a function to check every math function input for a zero and display the text “YOU CAN NOT ENTER A ZERO!” Add a function to check every math function input for a zero and display the text “YOU CAN NOT ENTER A ZERO!” Send email to rob.loy@scottsdalecc.edu with URL to calculator file before 6pm on September 26. Send email to rob.loy@scottsdalecc.edu with URL to calculator file before 6pm on September 26.rob.loy@s

25 Presentation Select a PHP built-in function from ths quick reference page at http://php.net/quickref.php Select a PHP built-in function from ths quick reference page at http://php.net/quickref.php http://php.net/quickref.php Send email to rob.loy@scottsdalecc.edu with your PHP function choice before September 26. First come, first approved for functions! Send email to rob.loy@scottsdalecc.edu with your PHP function choice before September 26. First come, first approved for functions!rob.loy@s Present your function syntax and sample on the screen Oct 10. Upload your code to your website and show results. Add some HTML text to show the syntax. Present your function syntax and sample on the screen Oct 10. Upload your code to your website and show results. Add some HTML text to show the syntax.

26 Sample PHP Presentation PHP Function Presentation Purpose: Pick a random element from an array. Syntax: $a=array(“Jan”,”Feb”,”Mar”,“Apr”, “May”); $b=array_rand($a); echo $b; Results:Feb


Download ppt "Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy."

Similar presentations


Ads by Google