Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 06.

Similar presentations


Presentation on theme: "COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 06."— Presentation transcript:

1 COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 06

2 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Scope Avoid clashes between variables in different functions. Each line of code belongs to a certain scope. Code that appears inside a function is considered to belong to the function's scope. Code that appears outside of any function is considered to belong to the global scope.

3 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Scope DEFINITION: The property that determines which memory table is used for storing variables and in turn which variables are accessible. Variables declared inside a function scope are local variables. private property of a function and may never be seen or manipulated outside the scope of the function. Variables used outside the scope of any function are global variables. Unlike some other languages, global variables in PHP are not immediately available outside the global scope.

4 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Scope <?php $name = "Leon"; //$name here is “global” – different to $name in assignName(). function assignName() { $name = "Zeev"; //$name is local to function assignName() } assignName(); print($name); //prints Leon ?>

5 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Scope The global statement brings a variable into a function's namespace. Thereafter the variable may be used as if it were outside the function. Any change to the variable will persist after execution of the function ceases. In the same way, it is possible to refer to global variables through the array GLOBALS. The array is indexed by variable names, so if you create a variable named userName, you can manipulate it inside a function by writing $GLOBALS[‘userName’].

6 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP <?php $capital = “Bridgetown"; function Nation() { global $capital; printCity($capital); } function printCity($CityName) { print("The city is $CityName.\n"); } function Dominica() { $capital = “Roseau"; printCity($capital); } function Antigua() { $capital = “St John"; printCity($capital); } Nation(); Antigua(); Dominica(); Nation(); ?> Scope – Example Output: Bridgetown St John Roseau Bridgetown

7 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Scope For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example: <?php $a = 1; include 'b.inc'; ?> Here the $a variable will be available within the included b.inc script.

8 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Scope Another way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. Example - Using $GLOBALS instead of global <?php $a = 1; $b = 2; function Sum() { $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b']; } Sum(); echo $b; ?>

9 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Scope The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Note that $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.

10 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Scope - Static Variables A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. <?php function Test() { $a = 0; echo $a; $a++; } ?> <?php function Test() { static $a = 0; echo $a; $a++; } ?>

11 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Recursion Functions may make calls to other functions; Functions may also make calls to themselves. The process of a function calling itself is recursion. This circular definition usually leads to elegant algorithms. The problem is broken down into a small task that's repeated many times. Recursive definitions are common in mathematics. e.g Consider the definition Fibonacci series or Factorials.

12 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Recursion Recursion is a difficult concept to understand. People use it because you can express an algorithm in fewer lines. Equivalent iterative algorithms usually must maintain this state on their own rather than relying on PHP to keep track of variables in the function for each call.

13 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Recursion - Factorial <?php function factorial ( $Number ) { if (($Number-1) < 0) return 0; elseif ($Number < 2) { return ( $Number ); } else { return ( $Number *factorial ( $Number -1));} } ?>

14 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Reading and Writing to Files Communication with files follows the pattern of opening a stream to a file, reading from or writing to it, and then closing the stream. When you open a stream, you get a resource that refers to the open stream. Each time you want to read from or write to the file, you use this stream identifier. PHP uses the number associated with the stream identifier to refer to all the necessary information for communicating with the file.

15 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Reading and Writing to Files To open a file on the local file system, you use the fopen function. It takes a name of a file and a string that defines the mode of communication. Basic Modes are: ‘r’ for read-only ‘w’ for write-only ‘a’ for appending Full description following…

16 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP modeDescription 'r'Open for reading only; place the file pointer at the beginning of the file. 'w'Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a'Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'r+'Open for reading and writing; place the file pointer at the beginning of the file. 'w+'Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a+'Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'x'Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error. 'x+'Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error. If the file does not exist, attempt to create it.

17 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Reading and Writing to Files Two other commonly used functions accessing file streams are: popen – which opens a pipe connection fsockopen - which opens a socket connection with the function. We shall concentrate on the fopen function

18 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Reading and Writing to Files Format for opening a file: = fopen(, ) Example - Opening Datafile.data for reading: $myfile = fopen(“Datafile.dat”, “r” ); Once opened then you can use c-type file functions such as fgets, fscanf, feof, etc. on the file.

19 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP <?php // open file for writing $filename = "/tmp/data.txt"; if(!($myFile = fopen($filename, "w"))) { print("Error: "); print("'$filename' not created\n"); exit; } //write some lines to the file fputs($myFile, "Save this for later\n"); fputs($myFile, "Save this line too\n"); //close the file fclose($myFile); // open file for reading if(!($myFile = fopen($filename, "r"))) { print("Error:"); print("'$filename' cannot read\n"); exit; } while(!feof($myFile)) { //read a line from the file $myLine = fgets($myFile, 255); print("$myLine\n"); } //close the file fclose($myFile); ?> Reading and Writing to Files

20 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Classes and Objects


Download ppt "COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 06."

Similar presentations


Ads by Google