430 likes | 635 Views
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 somethingCode is executed on the se
E N D
1. Quick and Dirty Intro to PHP By David Choffnes
(content shamelessly ripped from the manual)
2. 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.
3. 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>
4. 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.
5. Basic syntax Escaping from HTML
Example 5-1. Ways of escaping from HTML
1. <? 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 . . ." %>
6. Basic Syntax Example 5-2. Advanced escaping
<?php
if ($expression) {
?>
<strong>This is true.</strong>
<?php
} else {
?>
<strong>This is false.</strong>
<?php
}
?>
7. 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";
?>
<?php echo "This is a test" ?>
8. 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
?>
9. 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
10. Scalars Very simple:
$foo = true; (boolean)
$foo = 20; (integer)
$foo = 3.1415; (float)
11. 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”;
12. Strings heredoc syntax
Example 6-2. Here doc string quoting example
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
?>
13. 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.
14. 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.
15. Arrays $arr[key] = value;
$arr[] = value;
// key is either string or nonnegative integer
// value can be anything
16. 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();
?>
17. Null The special NULL value represents that a variable has no value. NULL is the only possible value of type NULL.
18. 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.
19. 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)
20. 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]*’
21. 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.
22. 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;
?>
23. 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.
24. 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
25. 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.
26. 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.
27. 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();
28. Variable Scope Access to global variables inside functions
Explicitly declare variable as global
global $a, $b;
Use the $GLOBALS array
$foo = $GLOBALS[“a”]
29. 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.
30. 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’].
31. 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äu
</select>
<input type="submit">
</form>
32. 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);
33. Operators Pretty much all of the operators as in C++/Java/…
String comparison uses “==“
34. Control structures If, then, else, elseif, while, foreach, do…while, for, break, continue, switch, case, return
Supports backticks like Perl
35. 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
36. 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;
}
37. 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’);
?>
38. More on PHP Read the Manual!
Know your PHP version number!
Repeat after me:
http://www.PHP.net is your friend.
39. 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
40. Oracle Function List OCIDefineByName
OCIBindByName
OCILogon
OCIPLogon
OCINLogon
OCILogOff
OCIExecute
OCICommit
OCIRollback
OCINewDescriptor
OCIRowCount
41. Oracle Function List OCINumCols
OCIResult
OCIFetch
OCIFetchInto
OCIFetchStatement
OCIColumnIsNULL
OCIColumnName
OCIColumnSize
OCIColumnType
OCIServerVersion
OCIStatementType
OCINewCursor
42. Oracle Function List OCIFreeStatement
OCIFreeCursor
OCIFreeDesc
OCIParse
OCIError
OCIInternalDebug
OCICancel
OCISetPrefetch
OCIWriteLobToFile
OCISaveLobFile
OCISaveLob
43. Oracle Function List OCILoadLob
OCIColumnScale
OCIColumnPrecision
OCIColumnTypeRaw
OCINewCollection
OCIFreeCollection
OCICollAssign
OCICollAppend
OCICollAssignElem
OCICollGetElem
OCICollMax