Introduction of php

43
Introduction of PHP By pTutorial (www.ptutorial.com)

description

php tutorial

Transcript of Introduction of php

Page 1: Introduction of php

Introduction of PHP

By pTutorial(www.ptutorial.com)

Page 2: Introduction of php

What is PHP?• PHP stand for hypertext pre-processor.

# PHP is a general purpose server side scripting language that is basically used for web development.

# PHP script run on web server.

# PHP is used for dynamic web page creation.

#PHP can easily embedded with HTML.

Page 3: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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

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

}?>

Page 7: Introduction of 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:

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

Page 8: Introduction of php

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: Introduction of php

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: Introduction of php

Scalars

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

Page 11: Introduction of php

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: Introduction of php

Strings

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

Example of stringspanning multiple linesusing heredoc syntax.

EOD;?>

Page 13: Introduction of php

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: Introduction of php

Arraysarray( [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: Introduction of php

Arrays

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

// value can be anything

Page 16: Introduction of php

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: Introduction of php

Null

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

Page 18: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php

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: Introduction of php
Page 34: Introduction of php

Control structures

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

• Supports backticks like Perl

Page 35: Introduction of php

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: Introduction of php

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: Introduction of php

Variable functions• Example 12-1. Variable function example

<?phpfunction 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: Introduction of php

More on PHP

• Read the Manual!• Know your PHP version number!• Repeat after me:

http://www.PHP.net is your friend.

Page 39: Introduction of php

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: Introduction of php

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

Page 41: Introduction of php

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

Page 42: Introduction of php

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

Page 43: Introduction of php

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