Quick and Dirty Intro to PHP By David Choffnes (content shamelessly ripped from the manual)

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

PHP I.
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.
1 Database Driven Web Application Clients Application Servers including web servers Database Server Traditional client-server (2-tier architecture): client:
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.
Guide To UNIX Using Linux Third Edition
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
PHP PHP: Hypertext Preprocessing. What is PHP? PHP is a server-side scripting language designed specifically for the Web. An open source language PHP.
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft.
PHP: Hypertext Processor Fred Durao
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
1 Introduction to PHP. 2 What is this “PHP” thing? Official description: “PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source.
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.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
AIT 616 Fall 2002 PHP. AIT 616 Fall 2002 PHP  Special scripting language used to dynamically generate web documents  Open source – Free!!!  Performs.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Nael Alian Introduction to PHP
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Variables, Operators and Data Types. By Shyam Gurram.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
Introduction to CS520/CS596_026 Lecture Two Gordon Tian Fall 2015.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
PHP PHP: Hypertext Preprocesor Personal Home Page Tools.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Introduction to PHP Advanced Database System Lab no.1.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
PHP Arūnas Liuiza. PHP 101 What is PHP? Widely popular dynamic interpreted opensource programming language, aimed for web development Syntax is simmilar.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
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,
PHP-Basic By- Tanvi Raval. Pre-requisites Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
PHP. Outline  What is PHP?  What can PHP do?  PHP Basics  PHP and Forms  Cookies and Sessions  Database Connections  Command.
Radoslav Georgiev Telerik Corporation
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
Session 2 Basics of PHP.
Introduction to Dynamic Web Programming
Web Database Programming Using PHP
DBW - PHP DBW2017.
PHP (PHP: Hypertext Preprocessor)
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
PHP Introduction.
PHP.
Web DB Programming: PHP
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Intro to PHP.
Tutorial 6 PHP & MySQL Li Xu
Tutorial 10: Programming with javascript
Introduction to PHP.
PHP: Hypertext Preprocessor
PHP an introduction.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Quick and Dirty Intro to PHP By David Choffnes (content shamelessly ripped from the manual)

What is PHP? PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. –Write an HTML script with some embedded code to do something –Code is executed on the server.

What is PHP? An example: Example 1-1. An introductory example Example <?php echo "Hi, I’m a PHP script!"; ?>

Ridiculous DB support (and more) Writing a database-enabled web page is incredibly simple. The following databases are currentlysupported: –Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress, FrontBase, PostgreSQL, FilePro (read-only), mSQL, Solid Hyperwave, Direct MS-SQL, Sybase, IBM DB2, MySQL, Velocis, Informix, ODBC, Unix dbm DBX database abstraction extension –allows you to transparently use any database Supports ODBC, the Open Database Connection standard, Support for talking to other services using protocols such as LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows) and countless others.

Basic syntax Escaping from HTML –Example 5-1. Ways of escaping from HTML 1. This is a shortcut for " " echo ("some editors (like FrontPage) don’t like processing instructions"); 4.

Basic Syntax Example 5-2. Advanced escaping <?php if ($expression) { ?> This is true. <?php } else { ?> This is false. <?php } ?>

Instruction Separation Instructions are separated the same as in C or Perl - terminate each statement with a semicolon. The closing tag (?>) also implies the end of the statement, so the following are equivalent: <?php echo "This is a test"; ?>

Comments PHP supports C, C++ and Unix shell style comments <?php echo "This is a test"; // This is a one- line c++ style comment /* This is a multi line comment yet another line of comment */ echo "This is yet another test"; echo "One Final Test"; # This is shell- style style comment ?>

Types PHP supports eight primitive types. –Four scalar types: boolean integer floating-point number (float) string –Two compound types: array object –And finally two special types: resource NULL

Scalars Very simple: – $foo = true; (boolean) – $foo = 20; (integer) – $foo = ; (float)

Strings A string literal can be specified in three different ways. –single quoted Variables not expanded –double quoted $foo = 20; echo “The value of foo is $foo”;

Strings –heredoc syntax Example 6-2. Here doc string quoting example <?php $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; ?>

Arrays Specifying with array() –An array can be created by the array() language- construct. It takes a certain number of comma- separated key => value pairs. –A key is either a nonnegative integer or a string. If a key is the standard representation of a non-negative integer, it will be interpreted as such (i.e. ’8’ will be interpreted as 8, while ’08’ will be interpreted as ’08’). –A value can be anything. –If you omit a key, the maximum of the integer-indices is taken, and the new key will be that maximum +1. If no integer-indices exist yet, the key will be 0 (zero). If you specify a key that already has a value assigned to it, that value will be overwritten.

Arrays array( [key =>] value,... ) // key is either string or nonnegative integer // value can be anything Creating/modifying with square-bracket syntax –You can also modify an existing array, by explicitly setting values. –This is done by assigning values to the array while specifying the key in brackets. You can also omit the key, add an empty pair of brackets (" [] ") to the variable-name in that case.

Arrays $arr[key] = value; $arr[] = value; // key is either string or nonnegative integer // value can be anything

Objects Object Initialization –To initialize an object, you use the new statement to instantiate the object to a variable. <?php class foo { function do_foo() { echo "Doing foo."; } $bar = new foo; $bar->do_foo(); ?>

Null The special NULL value represents that a variable has no value. NULL is the only possible value of type NULL.

Type Juggling PHP does not require (or support) explicit type definition in variable declaration; A variable’s type is determined by the context in which that variable is used. –If you assign a string value to variable var, var becomes a string. If you then assign an integer value to var, it becomes an integer. Operators on multiple types do NOT change the types of the operands themselves; the only change is in how the operands are evaluated.

Type Juggling Example $foo = "0"; // $foo is string (ASCII 48) $foo += 2; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a float (3.3) $foo = 5 + "10 Little Piggies"; // $foo is integer (15) $foo = 5 + "10 Small Pigs"; // $foo is integer (15)

Variable Basics Variables in PHP are represented by a dollar sign followed by the name of the variable. Variable name is case-sensitive. Variable names follow the same rules as other labels in PHP. –A valid variable name starts with a letter or underscore Followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ’[a-zA- Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*’

Variables Example $var = "Bob"; $Var = "Joe"; echo "$var, $Var"; // outputs "Bob, Joe" $4site = ’not yet’; // invalid; starts with a number $_4site = ’not yet’; // valid; starts with an underscore $täyte = ’mansikka’; // valid; ’ä’ is ASCII 228.

References Example <?php $foo = ’Bob’; // Assign the value ’Bob’ to $foo $bar = &$foo; // Reference $foo via $bar. $bar = "My name is $bar"; // Alter $bar... echo $foo; // $foo is altered too. echo $bar; ?>

PHP Variables $argv –Array of arguments passed to the script. $argc –Contains the number of command line parameters passed to the script (if run on the command line). $PHP_SELF –The filename of the currently executing script, relative to the document root. $HTTP_COOKIE_VARS –An associative array of variables passed to the current script via HTTP cookies. $_COOKIE –An associative array of variables passed to the current script via HTTP cookies. $HTTP_GET_VARS –An associative array of variables passed to the current script via the HTTP GET method.

PHP Variables $_GET –An associative array of variables passed to the current script via the HTTP GET method. $HTTP_POST_VARS –An associative array of variables passed to the current script via the HTTP POST method. $_POST –An associative array of variables passed to the current script via the HTTP POST method. $HTTP_POST_FILES –An associative array of variables containing information about files uploaded via the HTTP POST method. $_FILES –An associative array of variables containing information about files uploaded via the HTTP POST method

PHP Variables $HTTP_ENV_VARS –An associative array of variables passed to the current script via the parent environment. $_ENV –An associative array of variables passed to the current script via the parent environment. $HTTP_SERVER_VARS –An associative array of variables passed to the current script from the HTTP server. $_SERVER –An associative array of variables passed to the current script from the HTTP server.

PHP Variables $HTTP_SESSION_VARS –An associative array of session variables passed to the current script. $_SESSION –An associative array of session variables passed to the current script. $_REQUEST –An associative array merged from the GET, POST, and Cookie variables. In other words - all the information that is coming from the user, and that from a security point of view, cannot be trusted.

Scope Variables declared outside of functions, classes are global to the script, outside of function blocks Unlike C! Global variables are not automatically available to functions Example: $a = 1; /* global scope */ function Test() { echo $a; /* reference to local scope variable */ } Test();

Variable Scope Access to global variables inside functions –Explicitly declare variable as global global $a, $b; –Use the $GLOBALS array $foo = $GLOBALS[“a”]

Variable Variables (aka, Dave blows your mind) A variable variable takes the value of a variable and treats that as the name of a variable. In the above $a = "hello"; $$a = "world"; Two variables have been defined and stored in the PHP symbol tree: –$a with contents "hello" –$hello with contents "world“ echo "$a ${$a}"; produces the exact same output as: echo "$a $hello"; i.e. they both produce: hello world.

HTML Forms (GET and POST) When a form is submitted to a PHP script, any variables from that form will be automatically made available to the script by PHP. Located in the associative arrays $HTTP_POST_VARS, $HTTP_GET_VARS, and/or $HTTP_POST_FILES, according to the source of the variable in question. Example 7-1. Simple form variable Name: When the above form is submitted, the value from the text input will be available in $HTTP_POST_VARS[’username’].

More Complex HTML Forms Example 7-2. More complex form variables Name: Beer: Warthog Guinness Stuttgarter Schwabenbräu

Cookies! Set cookies using the setcookie() function. –Cookies are part of the HTTP header, so the SetCookie function must be called before any output is sent to the browser. Any cookies sent to you from the client will automatically be turned into a PHP variable just like GET and POST method data. If you wish to assign multiple values to a single cookie, just add [] to the cookie name. –For example: setcookie("MyCookie[]", "Testing", time()+3600);

Operators Pretty much all of the operators as in C++/Java/… String comparison uses “==“ TRUE if $a is equal to $b, and they Are of the same type Identical$a === $b TRUE if $a is equal to $b.Equal$a == $b ResultNameExample

Control structures If, then, else, elseif, while, foreach, do…while, for, break, continue, switch, case, return Supports backticks like Perl

Other stuff require(/path/to/file.php) –Includes and evaluates the specified file include(...) –Same thing include_once(...) –Makes sure same file is not included multiple times

User-defined functions A function may be defined using syntax such as the following: function foo ($arg_1, $arg_2,..., $arg_n) { echo "Example function.\n"; return $retval; }

Variable functions Example Variable function example <?php function foo() { echo "In foo() \n"; } function bar($arg = ”) { echo "In bar(); argument was ’$arg’. \n"; } $func = ’foo’; $func(); $func = ’bar’; $func(’test’); ?>

More on PHP Read the Manual! Know your PHP version number! Repeat after me: is your friend.

Oracle (finally) Two DBIs: –Oracle (old, deprecated, don’t use) –Oracle 8 <= PHP4 naming is different from PHP5 naming Old naming is deprecated in PHP5, but tlab-login has only PHP4

Oracle Function List OCIDefineByName OCIBindByName OCILogon OCIPLogon OCINLogon OCILogOff OCIExecute OCICommit OCIRollback OCINewDescriptor OCIRowCount

Oracle Function List OCINumCols OCIResult OCIFetch OCIFetchInto OCIFetchStatement OCIColumnIsNULL OCIColumnName OCIColumnSize OCIColumnType OCIServerVersion OCIStatementType OCINewCursor

Oracle Function List OCIFreeStatement OCIFreeCursor OCIFreeDesc OCIParse OCIError OCIInternalDebug OCICancel OCISetPrefetch OCIWriteLobToFile OCISaveLobFile OCISaveLob

Oracle Function List OCILoadLob OCIColumnScale OCIColumnPrecision OCIColumnTypeRaw OCINewCollection OCIFreeCollection OCICollAssign OCICollAppend OCICollAssignElem OCICollGetElem OCICollMax