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

Similar documents
Web Scripting using PHP

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

Lecture 12. PHP. cp476 PHP

PHPoC vs PHP > Overview. Overview

Web Scripting using PHP

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

Programming Languages: Part 2. Robert M. Dondero, Ph.D. Princeton University

The Design of C: A Rational Reconstruction: Part 2

Continued from previous lecture

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7

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

Programming Languages Part 2. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

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

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

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

C programming basics T3-1 -

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

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

B. V. Patel Institute of BMC & IT 2014

C - Basics, Bitwise Operator. Zhaoguo Wang

PHP Hypertext Preprocessor

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:

Lecture 7 PHP Basics. Web Engineering CC 552

Course Syllabus. Course Title. Who should attend? Course Description. PHP ( Level 1 (

CERTIFICATE IN WEB PROGRAMMING

Introduction to Programming Using Java (98-388)

PHP INTERVIEW QUESTION-ANSWERS

Contents. A Review of C language. Visual C Visual C++ 6.0

PHPoC Language Reference > Overview. Overview

Starting To Write PHP Code

- Origins - Rasmus Lerdorf Developed to allow him to track visitors to his Web site

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

CSC 1214: Object-Oriented Programming

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

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Introduction to Databases. Key Concepts. Calling a PHP Script 5/17/2012 PHP I

Standard File Pointers

File IO and command line input CSE 2451

Programming for the Web with PHP

PHP by Pearson Education, Inc. All Rights Reserved.

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

PHP. Interactive Web Systems

3. Java - Language Constructs I

Database Systems Fundamentals

Server side basics CSC 210

INTERVIEW QUESTIONS - PHP JOB 2014 (HTML)

13Holzner_index.qxd 3/23/05 1:51 PM Page 331. Index

211: Computer Architecture Summer 2016

CSC Java Programming, Fall Java Data Types and Control Constructs

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

An overview of Java, Data types and variables

Type Conversion. and. Statements

Shell programming. Introduction to Operating Systems

Context Free Grammar of KPL

File Handling in C. EECS 2031 Fall October 27, 2014

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

Web Programming Step by Step

Setting Up a Development Server What Is a WAMP, MAMP, or LAMP? Installing a WAMP on Windows Testing the InstallationAlternative WAMPs Installing a

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

The Design of C: A Rational Reconstruction (cont.)

Objects and Iterators

C-LANGUAGE CURRICULAM

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Creating HTML files using Notepad

Expressions & Flow Control

PHP CURRICULUM 6 WEEKS

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

- Origins - Rasmus Lerdorf Developed to allow him to track visitors to his Web site


Client-Side Web Technologies. JavaScript Part I

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Operators. Java operators are classified into three categories:

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Important Points about PHP:

Introduction to Programming (Java) 2/12

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

CS313D: ADVANCED PROGRAMMING LANGUAGE

25.2 Opening and Closing a File

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

CHIL CSS HTML Integrated Language

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

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

Object Oriented Programming. What is this Object? Using the Object s Slots

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

GNU ccscript Scripting Guide IV

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

PHP. Lab. de Bases de Dados e Aplicações Web MIEIC, FEUP 2010/11. Sérgio Nunes

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

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

Transcription:

Princeton University COS 333: Advanced Programming Techniques A Subset of PHP Program Structure <?php // Print "hello, world\n" echo "hello, world\n";?> ----------------------------------------------------------------------------------- <?php function sqr($i) return $i * $i; echo sqr(5). "\n";?> ----------------------------------------------------------------------------------- Typically embedded in a HTML document. Including include "file.php"; include_once "file.php"; require "file.php"; require_once "file.php"; Exec Continues File Included Upon Failure? Only Once? include y n include_once y y require n n require_once n y Building and Running php file.php Terminal I/O Reading from stdin: ivaluesread = fscanf(stdin, "%d %lf", $i, $d); Writing to stdout: icharswritten = printf("%d %f", $i, $d); Page 1 of 6

Writing to stderr: icharswritten = fprintf(stderr, "%d %f", $i, $d); Keywords and, or, xor, FILE, exception, LINE, array(), as, break, case, class, const, continue, declare, default, die(), do, echo(), else, elseif, empty(), enddeclare, endfor, endforeach, endif, endswitch, endwhile, eval(), exit(), extends, for, foreach, function, global, if, include(), include_once(), isset(), list(), new, print(), require(), require_once(), return(), static, switch, unset(), use, var, while, FUNCTION, CLASS, METHOD, final, php_user_filter, interface, implements, instanceof, public, private, protected, abstract, clone, try, catch, throw, cfunction, this, final, NAMESPACE, namespace, goto, DIR Primitive Data Types Integer: 1, 12345, 01, 012345, 0x1, 0x1DB5 Floating point number: 0.0, 1.23, 1.23e4 Interpreted string: "hi" Uninterpreted (raw) string: 'hi' Boolean: true, false (case insensitive) Null object: null (case insensitive) Conversions: (int), (integer): (bool), (boolean): (float), (double), (real): (string): (array): (class): (unset): cast to integer cast to boolean cast to float cast to string cast to array cast to class cast to NULL No declaration statements; variables are created when first assigned a value Operators non-associative clone, new (1) clone and new left [] (2) array() non-associative ++ -- (3) increment/decrement right ~ - (4) bitwise not, negation right (int) (float) (string) (4) typecast right (array) (object) (bool) @ (4) typecast non-associative instanceof (5) type check right! (6) logical left * / % (7) arithmetic left + -. (8) arithmetic and string left << >> (9) bitwise non-associative < <= > >= <> (10) comparison non-associative ==!= ===!== (11) comparison left & (12) bitwise and references left ^ (13) bitwise left (14) bitwise left && (15) logical left (16) logical left? : (17) conditional expression right = += -= *= /=.= %= (18) assignment Page 2 of 6

right &= = ^= <<= >>= => (18) assignment left and (19) logical left xor (20) logical left or (21) logical left, (22) many uses Statements Expression statement: expr; Echo statement: echo expr, expr,...; Compound statement: statement; statement;... Selection statements: if (booleanexpr) statement else statement; Note: false, 0, null, '', "", and array() all indicate logical FALSE; any other value indicates logical TRUE switch (expr) case (value1): statement;...; break; case (value2): statement;...; break;... default: statement;...; Iteration statements: while (booleanexpr) statement; do statement while (expr); for (initexpr; booleanexpr; increxpr) statement; Note: false, 0, null, '', "", and array() all indicate logical FALSE; any other value indicates logical TRUE foreach ($array as $element) statement; foreach ($associativearray as $key => $value) statement; break; continue; Return statement: Page 3 of 6

Function call: return; return expr; f(expr,...); Default is call-by-value. & preceding a formal parameter indicates call-by-reference. Exception handling: try... catch (Exception $e)... $e->getmessage()... throw new Exception("Description"); Classes, Objects, and Object References <?php class MyClass private $i; public function construct($i) $this->i = $i; public function get() return $this->i; public function set($i) $this->i = $i; function main() $obj1 = new MyClass(5); $obj1->set(10); echo $obj1->get(). "\n"; print_r($obj1);?> main(); Data Structures Arrays $a = array(1, 2, "three"); $a[] = "four"; // Appends. echo $a[0]; Associative Arrays $aa = array("john" => "rhythm guitar", "Paul" => "bass guitar"); $aa["george"] = "lead guitar"; $aa["ringo"] = "drums"; echo $aa["paul"]; Page 4 of 6

Functions on Arrays is_array(), count(), sort(), shuffle(), explode(), extract(), compact(), reset(), end() An array is not an object. Often must use call-by-reference when passing an array to a function. Strings function fill1($a) $a[] = 5; $a[] = 6; $a[] = 7; function fill2(&$a) $a[] = 5; $a[] = 6; $a[] = 7; $a = array(); fill1($a); // a is unaffected. fill2($a); // a is affected. Functions on Strings Files strlen(), strpos(), strtolower(), strtoupper(), chr(), ord(), explode(), implode(), ltrim(), rtrim(), trim(), substr(), str_replace(), ucwords() fopen(), fgets(), fscanf(), fprintf(), fclose() as in C $bool = file_exists($file); STDIN, STDOUT, and STDERR are predefined. Command-Line Arguments $argc is the argument count $argv is a list $argv[0] is the name of the program... if ($argc!= 3) echo "Usage: $argv[0] arg1 arg2"; exit(1); Superglobal Variables When run within a web server: $GLOBALS $_SERVER $_GET $_POST $_FILES $_COOKIE $_SESSION $_REQUEST $_ENV All variables Headers, paths, script locations, etc. Data passed to the script via the HTTP GET method Data passed to the script via the HTTP POST method Data uploaded to the script via the HTTP POST method Data passed to the script via HTTP cookies Session data Data from the browser, by default $_GET, $_POST, and $_COOKIE Data passed to the script via environment variables Page 5 of 6

Etc. We'll cover other features of PHP throughout the course as necessary. Copyright 2011 by Robert M. Dondero, Jr. Page 6 of 6