PHP Introduction
History • • • • • •
Conceived in fall of 1994 PHP Version 1 in spring 1995 PHP Version 2 1995-1997 PHP Version 3 1997-2000 PHP Version 4 in Q2 2000 Development Team (339 people) – Community effort!
Statistics • As of July 1, 2001 • out of 31,299,592 Domains – PHP installed on 22.2% of all domains • source: Netcraft
• out of 1,984,899 Apache Servers surveyed – 846,501 (42.65%) PHP • source: SecuritySpace.com
Platforms Platforms (current): UNIX (all variants) Win32 (NT/W95/W98/W2000) QNX MacOS (WebTen) OSX OS/2
Platforms (experimental): OS/390 AS/400
BeOS Server Interfaces: Apache module (UNIX,Win32) CGI fhttpd module ISAPI module (IIS, Zeus) NSAPI module (Netscape iPlanet) Java servlet AOLServer Roxen module thttpd module
Server Interfaces Experimental): Apache 2.0 module
Embedding HTML Embedding SGML style: XML style: ASP style: <% code %> Javascript style: <script language="php"> code <TITLE>Search results for "" Traditional CGI programming #!/usr/bin/perl print "\n"; print " <TITLE>Search results for \"$query\"\n"; print "\n"; print "\n";
Switching modes Syntax and switching modes:
You are using Internet Explorer You are not using Internet Explorer Output: You are not using Internet Explore
Form Handeling
Action.php Hi . You are years old.
C-like Syntax 0) { echo "+"; } echo "$loop
\n"; } while(--$loop) { switch($i % 2) { case 0: echo "Even
\n"; break; case 1: echo "Odd
\n"; break; } } do { echo "$loop
"; } while (++$loop < 10); ?>
Data types Basic data types numbers (integers and real) Decimal 1234, Octal 0777, Hex 0xff strings Double-quoted "abc", single-quoted 'abc' booleans true,false Dynamic typing Don't have to declare types Automatic conversion done Example Output: 1006.5
Arrays Ordered Arrays: Associative Arrays: Manipulating Sorting: sort(), rsort(), ksort(), usort(), array_multisort() Traversal: reset(), end(), next(), each(), current(), key(), array_walk() Advanced: array_diff(), array_intersect(), array_merge(), array_merge_recursive(), array_slice(), array_splice() and lots more...
Array Manipulation 'mouse','dog'=>'mailman');?>
$v ) { echo "\$arr[$k] = $v
\n"; } ?> Output: $arr[0] = 1 $arr[1] = foo $arr[2] = 1.57 $arr[cat] = mouse $arr[dog] = mailman
current($arr) next($arr) current($arr) prev($arr) end($arr) current($arr) key($arr) reset($arr) each($arr) each($arr) each($arr)
1 foo foo 1 mailman mailman dog 1 array(0,1) array(1,'foo') array(2,1.57)
User functions Pass by reference Default values <TITLE>
OOP items[$artnr] += $num; } } ?> Inheriting a class with a Constructor owner = $name; } } ?> Invocation: add_item(170923, 2);?>
Date/Time Functions Date Format
Definition
a
Prints am or pm (lowercase
A
Prints AM or PM (uppercase)
d
Prints numerical day of month with leading zeros (01,02,….)
D
Prints day of week as 3 letter abbrev (Mon, Tue…)
F
Prints name of Month (January, February…)
h
Prints hout in 12 hour format
H
Prints hour in 24 hour format
i
Prints the minutes (00-59)
j
Prints day of month, no leading zeros
l
Prints day of week (Monday, Tuesday…)
m
Prints number of the month (1 to 12)
M
Prints 3 char, abbrev of month (Jan, Feb…)
S
Prints the suffix for day of month (st,nd,rd,th)
U
Prints number of seconds since Jan,1 1970 00:00:00
y
Prints year as two digits
Y
Prints year as 4 digits
z
Prints out day of the year (222,303,350)
Date/Time Functions $today = date(“M j, Y”);
// Feb 11, 2002
$today = date(“F jS, Y”);
// February 11th, 2002
$today = date(“D F d, h:ia”);
// Mon February 11, 3:30 pm
$today = date(“l F jS”);
// Monday February 11th
Date/Time functions
Sep 23, 2001 22:29:16
= 1) { printf("Dmitry has been charged for %dday%s!
\n",$days,$days==1?'':'s'); } else { $hours = $diff / 3600; if ($hours >= 1) { printf("Dmitry has been in jail for %d hour%s!
\n",$hours,$hours==1?'':'s'); } else { echo "Dmitry is speaking at DefCon
\n"; } } ?> Dmitry has been charged for 69 days!
Converts between calendars (Julian, Gregorian, Mayan, etc)
String Manipulation substr Fastipulation explode This string had delimiters.
C-like file functions Reading from a file: "; } ?> Reading from a URL: Writing to a file:
Sessions To start a session use session_start() and to register a variable in this session use session_register().
The $HTTP_SESSION_VARS array contains all the variables in the current session. If register_globals is enabled (which it is by default) then the session variables will automatically be available in the global namespace as well.
Database Support SQL: Adabas D Empress IBM DB2 Informix Ingres Interbase Frontbase mSQL Direct MS-SQL MySQL ODBC Ovrimos Oracle (OCI7,OCI8) PostgreSQL Raima Velocis Solid Sybase DB++
Other: dBase filePro (read-only) dbm (ndbm, gdbm, Berkeley db)
SQL Example PHP scripts that talk to databases all look similar to the code below. Connect to the database, select a database, send a query and loop through the results. \n"; echo "
Name | Description |
---|
\n"; while ($a = mysql_fetch_array($result)) { echo "
$a[name] | ", "$a[descr] |
"; } echo ""; } else { echo "
Nothing to see here."; } ?>
DB Abstraction A database abstraction layer is bundled with PHP 4. In the example below, the only thing you would need to change to use a different database is the odbc word on the third line. prepare('SELECT * FROM comments'); $result = $db->execute($stmt); while($row = $db->fetchrow($result)) { while($row as $field => $value ) { echo "$field: $value
\n"; } } $db->disconnect(); ?>
New in Version 4 Better OO (syntax fixes, op. overloading, superclass ref.) Modular, self-contained extensions Database abstraction layer (ala Perl's DBI) Reference counting Thread safe core More server modules through SAPI foreach() Output buffering Support for a real debugger Support for caching compiled code Support for an optimizer Java ties