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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Objectives Using functions to organize PHP code
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Recursion. Binary search example postponed to end of lecture.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursion.
IS 1181 IS 118 Introduction to Development Tools Chapter 5 Reusing Code.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Intermediate PHP (2) File Input/Output & User Defined Functions.
ITM 352 © Port,KazmanFile I/O - 1 File I/O in PHP Persistent Data.
1 Storage Classes, Scope, and Recursion Lecture 6.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
Learners Support Publications Classes and Objects.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Working with arrays (we will use an array of double as example)
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
PHP Working with Files. Perancangan dan Pemrograman Web: PHP (wcw) Opening a File fopen (path_to_file, file_mode) File Modes File Mode Description ropen.
Introduction to File Processing with PHP - Part 2 Indexed Files.
I Power Higher Computing Software Development High Level Language Constructs.
PHP 4 Files, Functions. Opening a File The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
LECTURE 4 Files, File open, Read, Write. File Upload - In this lecture we will teach you how to open, read, and close a file on the server. - PHP Open.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
CHAPTER 8 PHP Advanced อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Files A collection of related data treated as a unit. Two types Text
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
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,
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
A First Book of ANSI C Fourth Edition
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Introduction to Recursion
C Functions -Continue…-.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Arrays, For loop While loop Do while loop
Topics Introduction to File Input and Output
<?php require("header.htm"); ?>
The structure of programming
Submitted By : Veenu Saini Lecturer (IT)
Topics Introduction to File Input and Output
ITM 352 Functions.
Presentation transcript:

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

COMP 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.

COMP 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.

COMP 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 ?>

COMP 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’].

COMP 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

COMP 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.

COMP 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; ?>

COMP 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.

COMP 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++; } ?>

COMP 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.

COMP 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.

COMP 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));} } ?>

COMP 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.

COMP 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…

COMP 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.

COMP 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

COMP 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.

COMP 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

COMP An Introduction to Computer Programming : University of the West Indies Introduction to PHP Classes and Objects