PHP Reusing Code and Writing Functions.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

PHP I.
Software Testing. Quality is Hard to Pin Down Concise, clear definition is elusive Not easily quantifiable Many things to many people You'll know it when.
Server-Side Includes (SSI) an Introduction and Demonstration presented by: Sean Conklin CMS Project Student-Assistant MWF mornings WA 406, x4200
Introduction to JavaScript
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Ruby (on Rails) CSE 190M, Spring 2009 Week 3. Web Programming in Ruby Ruby can be used to write dynamic web pages Similar to PHP, chunks of Ruby begins.
Chapter 2: Modularization
Programming Types of Testing.
Programming Logic and Design Fourth Edition, Introductory
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
DT211/3 Internet Application Development
DT228/3 Web Development JSP: Directives and Scripting elements.
Chapter 1 Principles of Programming and Software Engineering.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
IS 1181 IS 118 Introduction to Development Tools Chapter 5 Reusing Code.
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
Server Side Scripting Norman White. Where do we do processing? Client side – Javascript (embed code in html) – Java applets (send java program to run.
CH07: Writing the Programs Does not teach you how to program, but point out some software engineering practices that you should should keep in mind as.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Copyright © Curt Hill PhP History and Introduction.
Architecture Of ASP.NET. What is ASP?  Server-side scripting technology.  Files containing HTML and scripting code.  Access via HTTP requests.  Scripting.
Comp 245 Data Structures Software Engineering. What is Software Engineering? Most students obtain the problem and immediately start coding the solution.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
chap13 Chapter 13 Programming in the Large.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Modular Programming. Modular Programming (1/6) Modular programming  Goes hand-in-hand with stepwise refinement and incremental development  Makes the.
Introduction to Applets CS 3505 Client Side Scripting with applets.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
CSE 219 Computer Science III Program Design Principles.
Chapter 6 Server-side Programming: Java Servlets
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Software Engineering and Object-Oriented Design Topics: Solutions Modules Key Programming Issues Development Methods Object-Oriented Principles.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ASP-2-1 SERVER AND CLIENT SIDE SCRITPING Colorado Technical University IT420 Tim Peterson.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
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,
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
PHP using MySQL Database for Web Development (part II)
Principles of Programming and Software Engineering
>> PHP: HTML Integration
UNIT - V STORED PROCEDURE.
Topic: Functions – Part 2
PHP Introduction.
Starting Out with Programming Logic & Design
Building Web Applications
Introduction to Classes and Objects
Lecture 5: Functions and Parameters
Starting Out with Programming Logic & Design
Server Side Includes Server Side Includes (SSI) are simply this:
CMSC 345 Programming.
Presentation transcript:

PHP Reusing Code and Writing Functions

Reusing Code. Functions. Topics: Code inclusion using require() and include() Defining functions Managing function parameters Passing-by-reference vs. passing-by-value Parameters default values Variable-length parameter lists Understanding variable scope in functions Using functions to return values Using recursive functions Anonymous functions

Reusing Code - why? Advantages: Reduces costs Increases reliability Reusing eliminates time spent to design, code, test, debug a new piece of software. Increases reliability Existing, mature code is usually more reliable than new code: it has already been thoroughly tested, possible defects have been discovered during testing (and use), and have been fixed. Improves consistency Existing code is already consistent with the other parts of the system: in terms of user interfaces, interfaces with other systems.

Modularity - why? … has a favorable impact on program development: Allows program to be divided into logical pieces: Level of difficulty grows with the size and complexity of a program Divide work among programmers Easier to debug when modules are tested alone fully first, then integrated systematically Easier to read, especially when commented correctly. Easier to modify, isolating modules to change Allows for reuse (avoiding redundant code); when an algorithm or computation is done over and over, it can be put in a method.

Code Inclusion PHP allows to reuse any type of code (not only functions, classes): Can insert code from a file into your PHP script with include (‘filename’); require (‘filename’); Statements include() & require() are similar, except when they fail: include() construct just gives a warning if failing to include file require() construct gives a fatal error, that will cause program to terminate Can also use variations include_once() or require_once() to avoid problems with redundancy; ex. redefining same function Slower than include()/require()

Fail_include.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/fail_include_php.pdf http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/fail_include.php

Code Inclusion The files loaded using include / require can contain everything that is normally used in a PHP file: PHP statements, PHP functions, PHP classes, HTML code, client-side scripting PHP code still has to be placed within PHP tags Filename extensions: When include()/require() is used in a PHP script: the loaded file becomes part of the PHP script and is executed as such → as if the loaded file’s contents replaced the include()/require() statement PHP does not look at the filename extension of an included file  include files can have any extension, if not to be used directly. Usually: .php or .inc (note: if located in the web document tree, their content can be seen in clear!)

Main.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/main_php.pdf http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/reusable_php.pdf http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/main.php

Home.html http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/home.html http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/home.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/home_php.pdf http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/header_php.pdf http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/footer_php.pdf

Code Inclusion The include files can be dynamic and generate on-the-fly parts of the page Apache or PHP can be configured to automatically load specific pages / files before and after every page, even for individual directories (applications)  include() statements not needed in this case Not on cscdb…