PHP (cont.). 2 Functions Definition – function name(arguments){... } –syntax name as for a variable, but without $ arguments as for variables –to return.

Slides:



Advertisements
Similar presentations
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Advertisements

HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
Objectives Using functions to organize PHP code
PHP –Writing Reusable Code 10 March 2006 Adina Crainiceanu IT420: Database Management and Organization.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
DT228/3 Web Development JSP: Directives and Scripting elements.
JavaScript, Fourth Edition
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 2- JavaScript in Action Modern JavaScript Design And Develop Copyright © 2012 by Larry.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions.
Course Textbook: Build Your Own ASP.Net Website: Chapter 2
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Javascript and the Web Whys and Hows of Javascript.
4.1 JavaScript Introduction
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
WaveMaker Visual AJAX Studio 4.0 Training Troubleshooting.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Introduction to Applets CS 3505 Client Side Scripting with applets.
1 JavaScript in Context. Server-Side Programming.
Tutorial 10 Programming with JavaScript
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction to Exception Handling and Defensive Programming.
HTML Table and PHP Array
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Where does PHP code get executed?. Where does JavaScript get executed?
PHP Open source language for server-side scripting Works well with many databases (e.g., MySQL) Files end in.php,.php3 or.phtml Runs on all major platforms.
THINKING BIG Abstraction and Functions chapter 6 Modified by Dr. Paul Mullins for CPSC 130, F05.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Web Development 101 Presented by John Valance
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
1 Server versus Client-Side Programming Server-SideClient-Side.
1 JavaScript in Context. Server-Side Programming.
1) PHP – Personal Home Page Scripting Language 2) JavaScript.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Chapter 8 Error Handling and Debugging. Debugging “When you get frustrated, step away from the computer!”
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual Basic nd Edition.
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,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Introduction to.
Programming Web Pages with JavaScript
Introduction to Dynamic Web Programming
Tutorial 10 Programming with JavaScript
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
PHP.
Tutorial 6 PHP & MySQL Li Xu
Chapter 20: Programming Functions
Tutorial 10: Programming with javascript
Error Handling and Debugging
JavaScript: Introduction to Scripting
Presentation transcript:

PHP (cont.)

2 Functions Definition – function name(arguments){... } –syntax name as for a variable, but without $ arguments as for variables –to return a value use return expression; Call – name(arguments) –the number of arguments must match

3 Function Examples function copyright() { echo " Copyright ICS 2010 "; } function toFahrenheit($celsius) { return $celsius * 9 / ; } function loanPayment($amount,$interest,$years) { $power = pow($interest / , $years * 12) return $amount * $interest * $power / ($power – 1); }

4 Returning Multiple Values Multiple values can be returned as an array – return array(values-list); –e.g., function today() { return array('We', '4', '1', '2010', 'fools day'); } To assign the returned values to variables, use list() –e.g., list ($weekday, $month, $day, $year, $event) = today();

5 Default Argument A value can be assigned to an argument –e.g., » function mortgage($amount,$interest,$years=30) {... } Such argument can be omitted in the function call –e.g., » $monthlyPayment = mortgage(200000,0.05);

6 Scope If a variable is defined outside of functions –its scope is the page where it is defined –it is not accessible within a function unless it is declared there as global, e.g. – function f () { global $var; } avoid global variables If a variable is defined within a function –its scope is that function –it is not accessible outside of that function –an argument is such a variable, too

7 External files typically used for –often used code CSS JavaScript scripts PHP functions PHP with HTML snippets common to multiple pages –PHP classes include_once(file-path) –absolute or relative path –also include(), require_once(), require() –file needs –inserts file's text, i.e., scope rules persist

8 Form Handling Revisited One PHP file can define a form and handle it –use a branch to test whether the form was displayed –e.g., $isset($_REQUEST['respond']) respond is the name attribute of a hidden form element: – i.e. – and } ?>

9 Bugs Errors: –syntax easy to correct –exceptions/run-time harder to interpret and to find the cause the cause may happen earlier –logical hardest to find may not be detected, only results are wrong

10 Debugging To make PHP display error messages –set display_errors : ini_set('display_errors', 1); see textbook Table 7.1 on p. 208 for error levels Rules: –Don't change code without clear reason & goal –Try to figure out the error just by reading your code –Explain your code to someone else (even if they don't know PHP) –Take a break

11 Debugging Tips Make sure you –edit the right file –have saved changes –run PHP pages via URL Try –different browser –different server

12 Debugging Tips (cont.) Validate HTML –validator.w3.orgvalidator.w3.org Visualize –e.g. show table borders Print values of key variables –use echo, print_r(), var_dump() Comment out possibly offending code Use debugging plugins –Firebug in Firefox

13 Handling Errors You should handle errors gracefully –use die(message) to abort script execution –it can be used in a condition, e.g., include('code.php') OR die("Can't find 'code.php'");

14 Error Handler To make a custom error handler –define function myHandler ($number,$message,$file,$line,$vars){... } –then install it set_error_handler('myHandler'); –see textbook pp.212