PHP – Get & Post; Functions; and Arrays IS6116 – 07th February 2011 Forms and PHP • 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 $_POST • 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 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> 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> $_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) 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> 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> 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! Multiform inputs <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 <?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> page2.php <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 final.php <?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 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. 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” 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! Simple function without arguments • function say_hello() { echo “<p>Hello everybody!</p>”; } • To call the function: say_hello(); Function requiring an Argument • <?php function printBR($txt) { echo $txt.”<br/>”; } printBR(“Line one!”); printBR(“Line two!”); ?> 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 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; function $test(){ $testvariable = “this is a test variable”; } echo “test variable is “.$testvariable; • Will create an error! 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(); 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(); 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 Passing by Value - Example • <?php function addFive($num){ $num += 5; } $originalNum = 10; addFive($orignalNum); echo $originalNum; //Outputs 10 ?> 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 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 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) Array Example • $colours = array (“red”, “blue”, “green”, “yellow”, “black”, “purple”); • Same as: $colours[] = “red”; $colours[] = “blue”; $colours[] = “green”; and so on.... 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’]; Multidimensional Arrays • Facilitates storing arrays of arrays! • Extends beyond the associative array to store multiple sets of associative arrays Multidimensional Example • <?php $person = array( array(“name” => “John”, “age” => 19), array(“name” => “Mary”, “age” => 30), array(“name” => “Aine”, “age” => 23) ); ?> 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! Multidimensional Arrays • foreach ($person as $p) { while (list($n, $a) = each ($p)) { echo “Name: “.$n.”\n Age: ”.$a.”<br/>”; } } • Output: Name: John Age: 19 Name: Mary Age: 30 Name: Aine Age: 23 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