PHP –Writing Reusable Code 10 March 2006 Adina Crainiceanu www.cs.usna.edu/~adina IT420: Database Management and Organization.

Slides:



Advertisements
Similar presentations
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS.
Objectives Using functions to organize PHP code
ITC 240: Web Application Programming SUBHASH PRAJAPATI 04/14/15.
IS 1181 IS 118 Introduction to Development Tools Chapter 5 Reusing Code.
FILE UPLOADS CHAPTER 11. THE BASIC PROCESS 1.The HTML form displays the control to locate and upload a file 2.Upon form submission, the server first stores.
Unit 7 – Working with Forms 1. Creating a form 2. Accessing the submitted data 3. Common operations on forms.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.
Intro to Web Programming using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
INTERNET APPLICATION DEVELOPMENT For More visit:
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Thursday, October 18, 2012 Session 7: PHP.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
Internet and Web Application Development Revision.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
MySQL and PHP 3 March 2006 Adina Crainiceanu IT420: Database Management and Organization.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Storing and Retrieving Data
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
PHP and SQL Server: Connection IST2101. Typical web application interaction (php, jsp…) database drivers 2IST210.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
 A PHP script can be placed anywhere in the document.  A PHP script starts with  The default file extension for PHP files is ".php".  A PHP file normally.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
NMD202 Web Scripting Week2. Web site
PHP (cont.). 2 Functions Definition – function name(arguments){... } –syntax name as for a variable, but without $ arguments as for variables –to return.
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,
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
CGS 3066: Web Programming and Design Spring 2016 PHP.
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Basic HTML Page 1. First Open Windows Notepad to type your HTML code 2.
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.
PHP and SQL Server: Connection IST 210: Organization of Data IST2101.
DBW - PHP DBW2017.
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
Getting web pages First we need to get the webpage by issuing a HTTP request. The best option for this is the requests library that comes with Anaconda:
PHP: Database connection
PHP an introduction.
ITM 352 Functions.
Presentation transcript:

PHP –Writing Reusable Code 10 March 2006 Adina Crainiceanu IT420: Database Management and Organization

Goals Today  Writing Reusable code  External files  Functions

Typical Function Presentation  string date (string format [, int timestamp]) Return type Function name Parameter type Parameter name Optional parameter(s)

Calling a Function  $todayDate = date(“d F y”);  Note:  Function names are NOT case sensitive  Variable names are case senstive

Writing Reusable Code  Example: Typical page header: My Page Name  Type same code for every page?  What if want to change bgcolor?

Include files  Write code once and save it into a file  Include the file in every page  Example: header.php

Include example (cont)  index.php <?php $pageTitle = “Adina’s Page”; include (“header.php”); echo “Some content for my page”; ?>

Generated HTML Page Adina’s Page Some content for my page

Include Functions  include(string fileName)  Includes the content of fileName into current file  require(string fileName)  Like include()  Fatal error if fileName not found!  include_once(string fileName)  require_once(string fileName)

Class Exercise  Write the code for a footer in a file  Include it in index.php file, to obtain a well- formed HTML document

User-Defined Functions  Define functions to performs repetitive tasks  Examples:  Open a database connection and select a database  Display the elements of an array as a table  …

Define a Function  function my_function() { echo ‘This is printed by my function’; } Keyword to define a function Function name Function code

Calling the Function Result: This is printed by my function

Function Parameters  function my_function($text) { echo $text; }  Call: my_function(“Print this text”);  Result: Print this text

Optional Parameters  function my_function($text=“Default text”) { echo $text; }  Call: my_function(“Print this text”);  Result: Print this text  Call: my_function();  Result: Default text

Multiple Optional Parameters  function start_table($border, $cellspacing=2, $cellpadding=2){ echo “<table border = $border cellspacing = $cellspacing cellpadding = $cellpadding>”; }  start_table(1) equivalent start_table(1,2,2)  start_table(2,3) equivalent start_table(2,3,2)  start_table(2,3,4)  Parameter values filled in from left to right!

Return Values  function add_values($a, $b){ $result = $a + $b; return $result; }  Call: $added_val = add_values(4,5);  Result: $added_val has value 9

Class Exercise  Write a function is_leap to test whether a yar is a leap year or not  Return “Yes” if input parameter is leap year  Return “No” if input parameter is not a leap year

Variables Scope  Variables declared in functions are visible from declaration line to end of function – local variables  Variables declared outside functions are visible from declaration line to end of file, but not inside functions – global variables  Superglobal variables ($_POST, $_SERVER, …) are visible everywhere  Keyword global makes local variables global – not recommended

Variables Scope Example  function fn(){ $var = ‘content’; } fn(); echo $var;  Result?  Nothing is printed!

Variables Scope Example 2 $var = ‘content 1 ’; echo $var; function fn(){ echo $var; $var = ‘content 2 ’; echo $var } fn(); echo $var;  Result? content 1 content 2 content 1

Class Exercise  Write a function my_dbconnect to open a db connection and select a database  Input parameters:  db server name  user name – optional, default value “root”  password – optional, default value “”  database name  Return value:  FALSE if errors occurred  database connection if everything OK

Class Exercise  Write the PHP script to use my_dbconnect  Connect to localhost  Default user and password  Select database vp5fund