Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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');
12
Lecture 5: CGI and Scripts
CGI - Common Gateway Interface 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
13
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. 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:
14
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
15
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
16
Lecture 5: CGI and Scripts
Results are sent back to browser and displayed in the Web browser See getpost.html and getpost.php
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.