Lecture 5: Functions and Parameters

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

PHP syntax basics. Personal Home Page This is a Hypertext processor It works on the server side It demands a Web-server to be installed.
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.
TCP/IP Protocol Suite 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 22 World Wide Web and HTTP.
Chapter 7: User-Defined Functions II
Copyright 2004 Monash University IMS5401 Web-based Systems Development Topic 2: Elements of the Web (g) Interactivity.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
INTRODUCTION TO WEB DATABASE PROGRAMMING
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
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.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
ES Module 5 Uniform Resource Locators, Hypertext Transfer Protocol, & Common Gateway Interface.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Java CGI Lecture notes by Theodoros Anagnostopoulos.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Website Development with PHP and MySQL Saving Data.
Chapter 6 Server-side Programming: Java Servlets
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
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,
CGS 3066: Web Programming and Design Spring 2016 PHP.
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,
2440: 141 Web Site Administration Web Forms Instructor: Joseph Nattey.
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
Session 2 Basics of PHP.
Servlets.
CS 330 Class 7 Comments on Exam Programming plan for today:
Introduction to Dynamic Web Programming
Section 6.3 Server-side Scripting
Chapter 7: User-Defined Functions II
WWW and HTTP King Fahd University of Petroleum & Minerals
CS 371 Web Application Programming
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
DBW - PHP DBW2017.
Chapter 10: Void Functions
Hypertext Transport Protocol
Section 17.1 Section 17.2 Add an audio file using HTML
Chapter 6 Server-side Programming: Java Servlets
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Intro to PHP & Variables
Tutorial (4): HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
METHODS AND BEHAVIORS AKEEL AHMED.
Chapter 27 WWW and HTTP.
6 Chapter Functions.
PHP.
JavaScript.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Intro to PHP.
Java Programming Language
PHP an introduction.
PHP-II.
Web Application Development Using PHP
Presentation transcript:

Lecture 5: Functions and Parameters General syntax: function name ( params ) { // statements // optional return statement } Parameters are optional Note that there is no return type in the header If no return statement is used, the function will return NULL If return is done, type could be anything Cannot overload function names Dynamic typing does not allow for disambiguation

Lecture 5: Functions and Parameters By default, parameters are passed by value Formal parameter is a copy of the actual parameter Changes to formal parameter do not affect the actual parameter If we add an ampersand before the formal parameter, we change it to pass by reference Like in C++, but not an option in Java Formal parameter is another name for the actual parameter Changes to the formal parameter also change the actual parameter

Lecture 5: Value vs. Reference Parameters Let's discuss this a bit Java has only value parameters This means we cannot alter the value stored in the actual parameter within the function However, in many cases the value passed into the function is a reference to an object This allows us to change the object that the reference refers to, but we cannot reassign the reference to a new object reference cannot change this can change this (mutate) data in object X

Lecture 5: Value vs. Reference Parameters Reference parameters allow the data in the object to be changed Assuming the parameter is a reference to an object They also allow the reference to be reassigned so that the actual parameter can reference a different object new object reference can also be reassigned can change this (mutate) old object X

Lecture 5: Value vs. Reference Parameters How about object value parameters? Can’t reassign original reference Can change original object (only ref is copied) Same as Java How about array reference parameters? Can reassign original array variable Can change original array Non-existent in Java How about array value parameters? Can’t reassign original array variable Can’t change original array (array itself is copied) Different from Java

Lecture 5: Value vs. Reference Parameters Implications: In Java we can alter objects, including the contents of arrays, via reference parameters However, the parameter itself (either a primitive value or a reference) cannot change If we want to reassign a variable within a method what can we do? Use instance variables Pass them in via an array (i.e. hack) In PHP we CAN modify the value of a parameter inside a function if we use reference parameters Be careful if you choose to do this Avoid inadvertent changes to arguments

Lecture 5: Value vs. Reference Parameters Other “by references” in PHP: Pass by reference in foreach loop foreach ($arrayvar as &$value) Now you can update the contents of the array by modifying $value More efficient than indexing into array Assign by reference $ref = &$var; Now $ref is another name, or an alias, for $var Both are bound to the same value Unsetting a reference unset($ref); Now the binding between $ref and $var is broken

Lecture 5: Functions and Parameters Variables within functions by default are local to the function Like method variables in Java or automatic variables in C++ The global declaration is used to state that a variable used in a function refers to an (existing) global variable rather than a (new) local variable The static declaration is used to state that the (new) variable has global lifetime but local scope A “global” that prevents polluting the namespace See ex10.php

Lecture 5: More on Functions Variable Functions: PHP Function names can be stored in and called through variables This allows the function that is used to be determined dynamically (at run time) Can be handy if different functions need to be called in different situations PHP functions can have default arguments If no actual parameter is supplied the default value is used See ex11.php

Lecture 5: Debugging Note Many situations that produce compilation or run-time errors in Java will not do so in PHP Ex: Accessing a variable that has no value: $count = $count + 1; Ex: Accessing a scalar as an array: $count = 42; $count[1] = ‘hello’; However, these situations will produce warnings, which we can elect to see or not see in the returned web page We can determine whether these warnings (and actual errors) are seen or not via .htaccess files

Lecture 5: Debugging Note These are configuration files that allow per directory configuration options for the server For example the settings: php_value display_errors 1 php_value display_startup_errors 1 will send PHP warnings back to the client browser And the settings: php_value display_errors 0 php_value display_startup_errors 0 will hide the warnings from the user Can also set per script options for the server ini_set('display_errors', '1'); ini_set('display_startup_errors', '1');

Lecture 5: CGI and Scripts CGI - Common Gateway Interface http://en.wikipedia.org/wiki/Common_Gateway_Interface http://tools.ietf.org/html/rfc3875 Interface for Web servers that interact with browsers, utilizing scripting languages and the HTTP (HyperText Transfer Protocol) Used to allow data interaction between user and server scripts Ex. Extracting data sent via HTTP requests and passing to scripts

Lecture 5: CGI and Scripts Two best known HTTP methods: GET and POST GET appends user input to URL and requests corresponding document server parses URL - first part is a program that it invokes, second part is parameters passed along Ex. http://cs1520.cs.pitt.edu/~nomad/php/bogus.php?arg1=wacky Recommended usage for safe and idempotent requests Safe: For retrieval only – has no side effects on the server Idempotent: Making N > 1 identical requests has the same effect as making only 1 request See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

POST Lecture 5: GET and POST sends data as a stream to script program more suitable for large amounts of data arguments are not shown in address but are still extracted and processed by server Used for requests that may alter / update the server i.e. NOT safe and NOT idempotent Ex: update a database Ex: submit a payment

Lecture 5: CGI and Scripts GET and POST are often used within HTML forms User enters data into form and then SUBMITS it Browser processes form and passes choices and information to the url specified Server invokes appropriate script utilizing requested method, extracting submitted data Most scripting languages (including PHP) have predefined ways to easily extract this data This data is used as input to the script

Lecture 5: CGI and Scripts Results are sent back to browser and displayed in the Web browser See getpost.html and getpost.php