cis20.2-spring2010-sklar-lecii.4 1

Similar documents
What is PHP? [1] Figure 1 [1]

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

PHP 1. Introduction Temasek Polytechnic

Web Scripting using PHP

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

PHP Hypertext Preprocessor

CSCB20 Week 8. Introduction to Database and Web Application Programming. Anna Bretscher* Winter 2017

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Lecture (05) PHP 01. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Spring 2016, Web Programming

CS6501 IP Unit IV Page 1

Chapter 7:- PHP. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

PHP. Introduction. PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 11 Introduction to PHP

Lecture 2 Unix and PHP. INLS 523 Web Databases Spring 2013 Rob Capra

Some things to watch out for when using PHP and Javascript when building websites

Web Engineering (Lecture 08) WAMP

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Web Scripting using PHP

CERTIFICATE IN WEB PROGRAMMING

Important Points about PHP:

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

CGS 3066: Spring 2015 JavaScript Reference

Lecture 4. Wednesday, January 27, 2016

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Programming for the Web with PHP

Lecture 12. PHP. cp476 PHP

PHP. Interactive Web Systems

JavaScript I Language Basics

PHP: The Basics CISC 282. October 18, Approach Thus Far

COMS 469: Interactive Media II

PHP Introduction. Some info on MySQL which we will cover in the next workshop...

Server side basics CS380

PHP by Pearson Education, Inc. All Rights Reserved.

Part I. Web Technologies for Interactive Multimedia

COMP284 Scripting Languages Lecture 9: PHP (Part 1) Handouts

PHP 5 Introduction. What You Should Already Know. What is PHP? What is a PHP File? What Can PHP Do? Why PHP?

PHPoC vs PHP > Overview. Overview

Introduction of PHP Created By: Umar Farooque Khan

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

PHP INTERVIEW QUESTION-ANSWERS

Lecture 3: Web Servers / PHP and Apache. CS 383 Web Development II Monday, January 29, 2018

Scripting Languages. Diana Trandabăț

Princeton University COS 333: Advanced Programming Techniques A Subset of PHP

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

CSC Web Programming. Introduction to JavaScript

PHP 5 if...else...elseif Statements

M275 - Web Development using PHP and MySQL

COMS 469: Interactive Media II

Course Topics. The Three-Tier Architecture. Example 1: Airline reservations. IT360: Applied Database Systems. Introduction to PHP

Options. Real SQL Programming 1. Stored Procedures. Embedded SQL

Chapter 11 Outline. A Simple PHP Example Overview of Basic Features of PHP Overview of PHP Database Programming. Slide 11-2

Computer Components. Software{ User Programs. Operating System. Hardware

UFCEKG Lecture 4 Server Side Scripting & PHP

3 The Building Blocks: Data Types, Literals, and Variables

COMP284 Scripting Languages Lecture 10: PHP (Part 2) Handouts

CS313D: ADVANCED PROGRAMMING LANGUAGE

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

CMPT 100 : INTRODUCTION TO

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

today cs3157-fall2002-sklar-lect05 1

And Parallelism. Parallelism in Prolog. OR Parallelism

"Charting the Course... Intermediate PHP & MySQL Course Summary

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Lecture 7 PHP Basics. Web Engineering CC 552

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

Programming for Experimental Research. Flow Control

COMS 469: Interactive Media II

Python Unit

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

CS 377 Database Systems. Li Xiong Department of Mathematics and Computer Science Emory University

B. V. Patel Institute of BMC & IT 2014

JavaScript Functions, Objects and Array

Subject Name: Advanced Web Programming Subject Code: (13MCA43) 1. what is PHP? Discuss different control statements

Chapter 2 Working with Data Types and Operators

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

USQ/CSC2406 Web Publishing

CS 11 java track: lecture 1

SCRIPTING, DATABASES, SYSTEM ARCHITECTURE

Visual C# Instructor s Manual Table of Contents

Desktop Command window

First Simple Interactive JSP example

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Module - P7 Lecture - 15 Practical: Interacting with a DBMS

Student, Perfect Final Exam May 25, 2006 ID: Exam No CS-081/Vickery Page 1 of 6

JavaScript CS 4640 Programming Languages for Web Applications

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

WINTER. Web Development. Template. PHP Variables and Constants. Lecture

Basics of Java Programming

COMP519 Web Programming Lecture 27: PHP (Part 3) Handouts

php Mr. Amit Patel Hypertext Preprocessor Dept. of I.T.

(Refer Slide Time: 01:40)

Transcription:

cis20.2 design and implementation of software applications 2 spring 2010 lecture # II.4 today s topics: introduction to php on-line documentation http://www.php.net (php home page) http://www.php.net/manual/en/ (online reference) cis20.2-spring2010-sklar-lecii.4 1

introduction to php history and background your first php program basics writing your own functions arrays classes I/O cis20.2-spring2010-sklar-lecii.4 2

history and background developed in the latter 1990 s originally created as Personal Home Page tools, by Rasmus Lerdorf at first, was a quick tool for embedding sql queries in a web page (v1.0) then structured code was added (v2.0), but with a buggy language parser official release (v3.0) fixed parser bugs - June 1998 by Jan 1999, 100,000 web pages were using php!!! php is better than CGI because: it runs as part of the web server process and doesn t require forking (unlike CGI) it runs faster than CGI it s faster to write... php was designed to run with apache web server on unix but also runs on windows and mac it s free! cis20.2-spring2010-sklar-lecii.4 3

php is coded in C has a well-defined API extensible the way it runs: a php engine is installed as part of a web server the engine runs the php script and produces html, which gets passed back to the browser cis20.2-spring2010-sklar-lecii.4 4

your first program(s) hello.php (plain php) hello2.php (php embedded in html) hello3.php (uses <?php start tag) cis20.2-spring2010-sklar-lecii.4 5

hello.php <? print "hello world!";?> cis20.2-spring2010-sklar-lecii.4 6

hello2.php <html> <body bgcolor=#000000 text=#ffffff> <? print "hello world!";?> </body> </html> cis20.2-spring2010-sklar-lecii.4 7

hello3.php <html> <body bgcolor=#000000 text=#ffffff> <?php print "hello world!";?> </body> </html> cis20.2-spring2010-sklar-lecii.4 8

basics php start and end tags: <?...?> also: <?php...?> semi-colon ends a statement (like C) string constants surrounded by quotes (") or ( ) you can embed multiple php blocks in a single html file variable names are preceded by dollar sign ($) user input is through html forms the language is case-sensitive, but calls to built-in functions are not (not sure if that s true for all built-in functions) identifiers are made of letters, numbers and underscore (_); and cannot begin with a number expressions are just like in C cis20.2-spring2010-sklar-lecii.4 9

data types integers floating-point numbers strings loosely typed (you don t have to declare a variable before you use it) conversion functions: intval, doubleval, strval, settype settype( <value>, <newtype> ) where newtype="integer", "double" or "string" typecasting: (integer), (string), (double), (array), (object) cis20.2-spring2010-sklar-lecii.4 10

operators mathematical: +, -, *, /, %, ++, -- relational: <, >, <=, >=, ==,!= logical: AND, &&, OR,, XOR,! bitwise: &,, ^ (xor), ~ (ones complement), >>, << assignment: =, =, -=, *=, /=, other:. concatenate -> references a class method or property => initialize array element index cis20.2-spring2010-sklar-lecii.4 11

conditionals (1) if/elseif/else: if ( <expression1> ) { <statement(s)> elseif ( <expression2> ) { <statement(s)> else { <statement(s)> cis20.2-spring2010-sklar-lecii.4 12

conditionals (2) tertiary operator: switch: <conditional-expression>? <true-expression> : <false-expression>; switch( <root-expression> ) { case <case-expression>: <statement(s)>; break; default: <statement(s)>; break; cis20.2-spring2010-sklar-lecii.4 13

loops while do-while for break: while ( <expression> ) { <statement(s)>; do { <statement(s)>; while ( <expression> ); for ( <initialize> ; <continue> ; <increment> ) { <statement(s)>; execution jumps outside innermost loop or switch cis20.2-spring2010-sklar-lecii.4 14

other exit() function halts execution, meaning that no more code (php or html) is sent to the browser built-in constants PHP_VERSION FILE, LINE TRUE = 1, FALSE = 0 M_PI = pi (3.1415927...) cis20.2-spring2010-sklar-lecii.4 15

writing your own functions declared just like C: function <name> ( args ) { <body> [return <value>] called just like C arguments (and local variables) are local, and don t exist when you exit the function; but you can use static to declare a variable so that when you call a function again, the value is retained use the global statement to declare global variables that you want to be able to access from within a function, or the GLOBALS array (which is like a perl hash) e.g., GLOBALS[ username ] recursion is okay, but be careful! example: colors.php cis20.2-spring2010-sklar-lecii.4 16

colors.php <html> <body bgcolor=#ffffff> <? // modified from figure 2-4 from "introduction to php" by leon atkinson function usecolor() { static $colorval; if ( $colorval == "#ff0000" ) { $colorval = "#0000ff"; else { $colorval = "#ff0000"; return( $colorval ); print "<table width=\"300\">"; for ( $count=0; $count < 10; $count++ ) { cis20.2-spring2010-sklar-lecii.4 17

$rowcolor = usecolor(); print "<tr><td bgcolor=\"$rowcolor\">"; print "row number $count</td></tr>"; print "</table>"?> </body> </html> cis20.2-spring2010-sklar-lecii.4 18

arrays indexed using [...] indeces can be integers or strings (like a perl hash) when strings are indeces, it s called an associative array array() function can be used to initialize an array e.g., $var = array( value0, value1, value2,... ); use the => operator to define the index: $var = array( 1=>value1, value2,... ); $var = array( "a"=>value1, "b"=>value2,... ); multidimensional arrays are okay (like C) example: arrays.php cis20.2-spring2010-sklar-lecii.4 19

arrays.php <html> <body bgcolor=#ffffff> <? // modified from figure 5-6 from "introduction to php" by leon atkinson $states = array( "CA","NY" ); print "here are the states:<br>"; for ( $i=0; $i<count( $states ); $i++ ) { print "-- $states[$i]<br>"; print "<p>"; $cities = array( "CA"=>array( "san francisco","los angeles" ), "NY"=>array( "new york","albany","buffalo" )); print "here are the CA cities:<br>"; for ( $i=0; $i<count( $cities["ca"] ); $i++ ) { print( "-- ".$cities["ca"][$i]."<br>" ); print "here are the NY cities:<br>"; cis20.2-spring2010-sklar-lecii.4 20

for ( $i=0; $i<count( $cities["ny"] ); $i++ ) { print( "-- ".$cities["ny"][$i]."<br>" ); print "<p>"; $states[] = "MA"; print "now here are the states:<br>"; for ( $i=0; $i<count( $states ); $i++ ) { print "-- $states[$i]<br>"; $cities[] = "MA"; $cities["ma"][] = "boston"; print "here are the MA cities:<br>"; for ( $i=0; $i<count( $cities["ma"] ); $i++ ) { print( "-- ".$cities["ma"][$i]."<br>" );?> </body> </html> cis20.2-spring2010-sklar-lecii.4 21

classes defining a class: class <class-name> { // declare properties // declare methods use just like java and c++ example: myclass.php and userclass.php note use of include statement cis20.2-spring2010-sklar-lecii.4 22

myclass.php <html> <body> <? // figure 6-2 from "introduction to php" by leon atkinson include "userclass.php"; $currentuser = new user; $currentuser->init( "yoda","jedi" ); print( "name = ".$currentuser->name."<br>" ); print( "last login = ".$currentuser->getlastlogin() );?> </body> </html> cis20.2-spring2010-sklar-lecii.4 23

userclass.php <? // figure 6-2 from "introduction to php" by leon atkinson class user { // properties var $name; var $password; var $last_login; // methods function init( $inputname, $inputpassword ) { $this->name = $inputname; $this->password = $inputpassword; $this->last_login = time(); function getlastlogin() { cis20.2-spring2010-sklar-lecii.4 24

return( date( "M d Y", $this->last_login )); cis20.2-spring2010-sklar-lecii.4 25

I/O get input from html forms using $_POST[ <name> ] $_GET[ <name> ] $_REQUEST[ <name> ] file I/O basically just like C: $fp = fopen( "filename","w" ); fwrite( $fp,"stuff" ); fclose( $fp ); note that fopen second argument mode is like C) cis20.2-spring2010-sklar-lecii.4 26

lunch.html <!-- figure 1-4 from "introduction to php" by leon atkinson --> <html> <body> <form action="lunch.php" method="post"> your name: <input type="text" name="yourname"><br> cost of lunch: <input type="text" name="cost"><br> days buying lunch: <input type="text" name="numdays"><br> <input type="submit"> </form> </body> </html> cis20.2-spring2010-sklar-lecii.4 27

lunch.php <? // figure 1-5 from "introduction to php" by leon atkinson $today = date("l F d, Y"); $yourname = $_POST[ yourname ]; $cost = doubleval( $_POST[ cost ] ); $numdays = intval( $_POST[ numdays ] );?> <html> <body> today is: <? print( "$today<br>" ); print( "$yourname, you will be out \$" ); print( doubleval( $cost * $numdays )); cis20.2-spring2010-sklar-lecii.4 28

print( " for buying lunch this week!" );?> </body> </html> cis20.2-spring2010-sklar-lecii.4 29