php – get & post; functions; and arrays is6116 – 07 th february 2011

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

Upload: rosaline-peters

Post on 26-Dec-2015

216 views

Category:

Documents


3 download

TRANSCRIPT

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

PHP – Get & Post; Functions; and Arrays

IS6116 – 07th February 2011

Page 2: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

• 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

Page 3: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

• 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

Page 4: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Example - $_POST

<html><body><form action="newpage.php" method="post">Name: <input type="text" name="name">Age: <input type="text" name="age"><input type="submit"></form></body></html>

Page 5: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Example - $_POST continued<html>

<head><title>Grab form values</title></head>

<body>Welcome <?php echo $_POST["name"];?>.<br/>

You are <?php echo $_POST["age"];?> years old

</body>

</html>

Page 6: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

$_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)

Page 7: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Example - $_GET

<html><body><form action="get.php" method="get">Name: <input type="text" name="name">Age: <input type="text" name="age"><input type="submit"></form></body></html>

Page 8: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Example - $_GET<html><head><title>Grab form values</title></head>

<body>Welcome <?php echo $_GET["name"];?>.<br/>

You are <?php echo $_GET["age"];?> years old

</body>

</html>

Page 9: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

htmlentities• An inbuilt function that takes a string and

returns the same string with HTML converted into HTML entities.

• For example, the string "<script>" would be converted to "&lt;script&gt;".

• Prevents the browser from using input values as HTML code!

Page 10: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

<HTML> <HEAD> <TITLE>Multi-page Form - Page One</TITLE> </HEAD> <BODY> <p>Please fill in the following information</p>

<FORM METHOD="POST" ACTION="page2.php"> Name: <INPUT TYPE="text" SIZE="40" name="cust_name"><BR> Email: <INPUT TYPE="text" SIZE="40" name="cust_email"><BR> <INPUT TYPE="submit" name="submit1" value="Proceed"> </FORM>

</BODY> </HTML>

10

Multiform inputs

Page 11: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

<?php $cust_name = htmlentities ($_POST['cust_name']); $cust_email = htmlentities ($_POST['cust_email']); ?> <HTML> <HEAD> <TITLE>Multi-page Form - Page Two</TITLE> </HEAD> <BODY> <p>Please fill in the following information</p>

<FORM METHOD="POST" ACTION="final.php">

Address: <INPUT TYPE="text" SIZE="50" name="cust_address"><br/> Phone: <INPUT TYPE="text" SIZE="20" name="cust_phone"><br/> <INPUT TYPE="hidden" name="cust_name" VALUE="<?php echo $cust_name; ?>"> <INPUT TYPE="hidden" name="cust_email" VALUE="<?php echo $cust_email; ?>"> <INPUT TYPE="submit" name="submit2" value="Proceed">

</FORM> </BODY> </HTML>

11

page2.php

Page 12: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

<?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']); ?> <HTML> <HEAD> <TITLE>Multi-page Form - Final</TITLE> </HEAD> <BODY> <p>You filled in:</p>

Name: <?php echo $cust_name; ?><br/> Email: <?php echo $cust_email; ?><br/> Address: <?php echo $cust_address; ?><br/> Phone: <?php echo $cust_phone; ?><br/>

</BODY> </HTML>

12

final.php

Page 13: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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.

Page 14: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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”

Page 15: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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!

Page 16: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Simple function without arguments

• function say_hello(){echo “<p>Hello everybody!</p>”;}

• To call the function:say_hello();

Page 17: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Function requiring an Argument

• <?phpfunction printBR($txt){echo $txt.”<br/>”;}

printBR(“Line one!”);printBR(“Line two!”);?>

Page 18: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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

Page 19: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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;

Page 20: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

function $test(){$testvariable = “this is a test variable”;}

echo “test variable is “.$testvariable;

• Will create an error!

Page 21: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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();

Page 22: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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();

Page 23: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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

Page 24: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Passing by Value - Example• <?php

function addFive($num){$num += 5;

}

$originalNum = 10;addFive($orignalNum);

echo $originalNum; //Outputs 10?>

Page 25: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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

Page 26: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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

Page 27: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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)

Page 28: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Array Example

• $colours = array (“red”, “blue”, “green”, “yellow”, “black”, “purple”);

• Same as: $colours[] = “red”; $colours[] = “blue”;

$colours[] = “green”; and so on....

Page 29: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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’];

Page 30: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Multidimensional Arrays

• Facilitates storing arrays of arrays!

• Extends beyond the associative array to store multiple sets of associative arrays

Page 31: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Multidimensional Example

• <?php$person = array(

array(“name” => “John”, “age” => 19),array(“name” => “Mary”, “age” => 30),array(“name” => “Aine”, “age” => 23));

?>

Page 32: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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!

Page 33: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

Multidimensional Arrays• foreach ($person as $p) {

while (list($n, $a) = each ($p)) {echo “Name: “.$n.”\n Age: ”.$a.”<br/>”;}

}

• Output:Name: JohnAge: 19Name: MaryAge: 30Name: AineAge: 23

Page 34: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011

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

Page 35: PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011