Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.

Similar presentations


Presentation on theme: "PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011."— Presentation transcript:

1 PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

2 The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input Form elements in an HTML/PHP page will automatically be available to your PHP scripts. 2 Forms and PHP

3 The $_POST variable is an array of variable names and values sent by the HTTP POST method The $_POST variable is used to collect values from a form with method="post“ Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send 3 $_POST

4 Example - $_POST Name: Age:

5 Example - $_POST continued Grab form values Welcome. You are years old

6 $_GET The $_GET variable is an array of variable names and values sent by the HTTP GET method The $_GET variable is used to collect values from a form with method="get" Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and it has limits on the amount of information to send (max. 100 characters)

7 Example - $_GET Name: Age:

8 Example - $_GET Grab form values Welcome. You are years old

9 htmlentities An inbuilt function that takes a string and returns the same string with HTML converted into HTML entities. For example, the string " " would be converted to "<script>". Prevents the browser from using input values as HTML code!

10 Multi-page Form - Page One Please fill in the following information Name: Email: 10 Multiform inputs

11 <?php $cust_name = htmlentities ($_POST['cust_name']); $cust_email = htmlentities ($_POST['cust_email']); ?> Multi-page Form - Page Two Please fill in the following information Address: Phone: <INPUT TYPE="hidden" name="cust_name" VALUE=" "> <INPUT TYPE="hidden" name="cust_email" VALUE=" "> 11 page2.php

12 <?php $cust_name = htmlentities($_POST['cust_name']); $cust_email = htmlentities($_POST['cust_email']); $cust_address = htmlentities($_POST['cust_address']); $cust_phone = htmlentities($_POST['cust_phone']); ?> Multi-page Form - Final You filled in: Name: Email: Address: Phone: 12 final.php

13 Functions 2 types: – Built in functions – Custom defined functions Functions minimize the amount of repetition of code. function consists of function name followed by parentheses. Some functions may need values passed to it.

14 Built in Functions Thousands of built in functions. Can be found on w3schools.com or php.net Example built-in function: strtoupper() strtoupper(“php on Mondays”); converts string “php on Mondays” to “PHP ON MONDAYS”

15 Custom Defined Functions Syntax: function name_of_function($arg1, $arg2) { //code of the function } Note: You can but are not required to include arguments within the parentheses!

16 Simple function without arguments function say_hello() { echo “ Hello everybody! ”; } To call the function: say_hello();

17 Function requiring an Argument <?php function printBR($txt) { echo $txt.” ”; } printBR(“Line one!”); printBR(“Line two!”); ?>

18 Function to return a value (s) <?php function addNums($first, $second) { $result = $first + $second; return $result; } echo addNums(10, 3); //Sends 10 and 3 as arguments to the addNums function and will print 13

19 Variable Scope Variables declared within functions remain local to those functions (i.e. they will not be accessible outside the function) function $test(){ $testvariable = “this is a test variable”; } echo “test variable is “.$testvariable;

20 function $test(){ $testvariable = “this is a test variable”; } echo “test variable is “.$testvariable; Will create an error!

21 Variable scope continued Variables defined outside functions are inaccessible within functions by default Following will output an error! $testvariable = “this is a test variable”; function $test(){ echo $testvariable; } test();

22 Variable Scope Continued Use global statement to access variable in a function that has been defined outside the said function $testvariable = “this is a test variable”; function $test(){ global $testvariable; echo $testvariable; } test();

23 Passing by value & by reference When passing arguments to functions, they are stored as copies Any changes made to the variable within the body of a function are local to that function and do not extend to outside

24 Passing by Value - Example <?php function addFive($num){ $num += 5; } $originalNum = 10; addFive($orignalNum); echo $originalNum; //Outputs 10 ?>

25 Passing by Reference - Example <?php function addFive(&$num){ $num += 5; } $originalNum = 10; addFive($orignalNum); echo $originalNum; //Outputs 15 ?> Passing by Reference gives access to the actual variable that is passed rather than just a copy of the variable as per passing by Value

26 Arrays Variables typically store one value at a time (i.e. a variable may store a single colour such as red, blue, green, or some other colour However, where you may need to store a list of values in a variable rather than just one value an array is suitable E.g. a normal variable may store a value for the colour variable whereas an array facilitates storing a list of values for an array

27 Creating arrays number of ways to create arrays – array() function (typically used when populating the values of array in one go!) – the array operator [] (typically to store one value initially allowing for additional values later)

28 Array Example $colours = array (“red”, “blue”, “green”, “yellow”, “black”, “purple”); Same as: $colours[] = “red”; $colours[] = “blue”; $colours[] = “green”; and so on....

29 Associative Arrays A variable array capable of storing various different elements and associated values $person = array(“name” => “John”, “age” => 10, “occupation” => “PHP Coder”); $person[‘age’] = 10; $person[‘name’ = “John”; echo $person[‘age’];

30 Multidimensional Arrays Facilitates storing arrays of arrays! Extends beyond the associative array to store multiple sets of associative arrays

31 Multidimensional Example <?php $person = array( array(“name” => “John”, “age” => 19), array(“name” => “Mary”, “age” => 30), array(“name” => “Aine”, “age” => 23) ); ?>

32 Multidimensional Arrays each of the array elements store starting at an index value of 0 for instance: – echo $person[0]; – echo $person[1]; – and so on and on and on!

33 Multidimensional Arrays foreach ($person as $p) { while (list($n, $a) = each ($p)) { echo “Name: “.$n.”\n Age: ”.$a.” ”; } Output: Name: John Age: 19 Name: Mary Age: 30 Name: Aine Age: 23

34 In built functions suitable for Arrays count() sizeof() each() list() foreach() array_push() - Adds 1 or more elements to the end of an existing array – e.g. array_push($person, “Harry”, 24); More array related functions – can be found at php.net/array or w3schools.com and so on

35


Download ppt "PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011."

Similar presentations


Ads by Google