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

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

PHP Form and File Handling
PHP I.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Everyday Italian Giada De Laurentiis Harry Potter J K. Rowling Learning XML Erik T. Ray CSCI 305 Introduction to Database.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
PHP (2) – Functions, Arrays, Databases, and sessions.
A simple PHP application We are going to develop a simple PHP application with a Web interface. The user enters two numbers and the application returns.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
PHP Forms. I. Using PHP with HTML Forms A very common application of PHP is to have an HTML form gather information from a website's visitor and then.
Introduction to scripting
04/09/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
COM336 – Web Database Development XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
PHP == ‘ Hypertext Preprocessor ’ Open-source, server-side scripting language Used to generate dynamic web-pages PHP scripts reside between reserved PHP.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
PHP : Hypertext Preprocessor
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
PHP Forms and User Input The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
INTERNET APPLICATION DEVELOPMENT For More visit:
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
13/2/12 Lecture 6. Functions 2 types: – Built in functions – Custom defined functions Functions minimize the amount of repetition of code. function consists.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
PHP Open source language for server-side scripting Works well with many databases (e.g., MySQL) Files end in.php,.php3 or.phtml Runs on all major platforms.
Introduction to PHP.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 3: Working with PHP Rob Gleasure
NMD202 Web Scripting Week2. Web site
Introduction to PHP 1.What is PHP? What Is PHP?  php: hypertext preprocessor  Server-side scripting language—like ASP—scripts are executed on server.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
Radoslav Georgiev Telerik Corporation
PHP – Hypertext Preprocessor.
A pache M ySQL P hp Robert Mudge Reference:
CGS 3066: Web Programming and Design Spring 2017
Session 2 Basics of PHP.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
Function There are two types of Function User Defined Function
8th Semester, Batch 2008 Department of Computer Science SSUET.
PHP Variables A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume) Rules for PHP variables: A variable.
PHP Introduction.
PHP FORM HANDLING Post Method
PHP.
Basics.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Lecture 5: Functions and Parameters
Web Programming Language
PHP-II.
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
Presentation transcript:

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

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

Example - $_POST Name: Age:

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

$_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 Name: Age:

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

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!

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

<?php $cust_name = htmlentities ($_POST['cust_name']); $cust_ = htmlentities ($_POST['cust_ ']); ?> 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_ " VALUE=" "> 11 page2.php

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

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 “ Hello everybody! ”; } To call the function: say_hello();

Function requiring an Argument <?php function printBR($txt) { echo $txt.” ”; } 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.” ”; } 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