quick and dirty intro to php by david choffnes (content shamelessly ripped from the manual)

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

Upload: garrett-leef

Post on 01-Apr-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

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

Quick and Dirty Intro to PHP

By David Choffnes(content shamelessly ripped from the manual)

Page 2: 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.

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

What is PHP?

• An example:• Example 1-1. An introductory example

<html><head>

<title>Example</title></head><body>

<?php echo "Hi, I’m a PHP script!";?>

</body></html>

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

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.

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

Basic syntax

• Escaping from HTML– Example 5-1. Ways of escaping from HTML1. <? echo ("this is the simplest, an SGML processing instruction\n"); ?>

<?= expression ?> This is a shortcut for "<? echo expression ?>"

2. <?php echo("if you want to serve XHTML or XML documents, do like this\n"); ?>

3. <script language="php"> echo ("some editors (like FrontPage) don’t like processing instructions");

</script>4. <% echo ("You may optionally use ASP-style tags"); %>

<%= $variable; # This is a shortcut for "<% echo . . ." %>

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

Basic Syntax

• Example 5-2. Advanced escaping<?phpif ($expression) {?><strong>This is true.</strong><?php

} else {?><strong>This is false.</strong><?php

}?>

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

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:

<?phpecho "This is a test";?><?php echo "This is a test" ?>

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

Comments

• PHP supports C, C++ and Unix shell style comments<?phpecho "This is a test"; // This is a one-line c++ style comment

/* This is a multi line commentyet another line of comment */echo "This is yet another test";echo "One Final Test"; # This is shell-style style comment

?>

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

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

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

Scalars

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

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

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”;

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

Strings

– heredoc syntaxExample 6-2. Here doc string quoting example<?php$str = <<<EOD

Example of stringspanning multiple linesusing heredoc syntax.

EOD;?>

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

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.

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

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.

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

Arrays

$arr[key] = value;$arr[] = value;// key is either string or nonnegative integer

// value can be anything

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

Objects

• Object Initialization– To initialize an object, you use the new statement to

instantiate the object to a variable.<?phpclass foo{

function do_foo(){

echo "Doing foo.";}

}$bar = new foo;$bar->do_foo();

?>

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

Null

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

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

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.

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

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)

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

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]*’

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

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.

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

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;

?>

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

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.

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

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

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

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.

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

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.

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

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();

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

Variable Scope

• Access to global variables inside functions– Explicitly declare variable as global

global $a, $b;– Use the $GLOBALS array

$foo = $GLOBALS[“a”]

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

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.

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

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<form action="foo.php" method="post">Name: <input type="text" name="username"><br><input type="submit"></form>

• When the above form is submitted, the value from the text input will be available in $HTTP_POST_VARS[’username’].

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

More Complex HTML Forms

• Example 7-2. More complex form variables<form action="array.php" method="post">Name: <input type="text" name="personal[name]"><br>

Email: <input type="text" name="personal[email]"><br>

Beer: <br><select multiple name="beer[]"><option value="warthog">Warthog<option value="guinness">Guinness<option value="stuttgarter">Stuttgarter Schwabenbr&auml;u

</select><input type="submit"></form>

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

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);

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

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

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

Control structures

• If, then, else, elseif, while, foreach, do…while, for, break, continue, switch, case, return

• Supports backticks like Perl

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

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

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

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;

}

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

Variable functions

• Example 12-1. Variable function example<?php

function foo(){

echo "In foo()<br>\n";}function bar($arg = ”){

echo "In bar(); argument was ’$arg’.<br>\n";}$func = ’foo’;$func();$func = ’bar’;$func(’test’);

?>

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

More on PHP

• Read the Manual!

• Know your PHP version number!

• Repeat after me: http://www.PHP.net is your friend.

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

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

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

Oracle Function List

• OCIDefineByName• OCIBindByName• OCILogon• OCIPLogon• OCINLogon• OCILogOff • OCIExecute• OCICommit • OCIRollback• OCINewDescriptor• OCIRowCount

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

Oracle Function List

• OCINumCols• OCIResult• OCIFetch• OCIFetchInto• OCIFetchStatement• OCIColumnIsNULL• OCIColumnName• OCIColumnSize• OCIColumnType • OCIServerVersion• OCIStatementType• OCINewCursor

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

Oracle Function List

• OCIFreeStatement• OCIFreeCursor• OCIFreeDesc• OCIParse• OCIError• OCIInternalDebug • OCICancel• OCISetPrefetch• OCIWriteLobToFile• OCISaveLobFile• OCISaveLob

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

Oracle Function List

• OCILoadLob• OCIColumnScale• OCIColumnPrecision• OCIColumnTypeRaw• OCINewCollection• OCIFreeCollection• OCICollAssign• OCICollAppend• OCICollAssignElem• OCICollGetElem• OCICollMax