PHP Introduction - Chettinad College of Engineering & Technology

Download Report

Transcript PHP Introduction - Chettinad College of Engineering & Technology

Introduction to Open Source
Sri Eshwar College Of
Engineering
1
Definitions: Open Source Software
• Open Source
Software (OSS)
• Open Source doesn't
just mean access to
the source code.
That is an important
part of it, but not all
of it.
2
Definitions: Free Software
• Free Software “is
software that comes
with permission for
anyone to use, copy,
and distribute, either
verbatim or with
modifications, either
gratis or for a fee. In
particular, this means
that source code must
be available.”
3
Definitions: Open Source Software
•
•
•
•
•
Linux
Apache
MySQL
Perl /PHP
LAMP
4
An Introduction to PHP
PHP
PHP
PHP
PHP
What does PHP stand for?
stands for ‘PHP Hypertext Preprocessor’
What does PHP stand for?
stands for ‘PHP Hypertext Preprocessor’
What does PHP stand for?
stands for ‘PHP Hypertext Preprocessor’
What does PHP stand for?
stands for ‘PHP Hypertext Preprocessor’
5
What is PHP?
– PHP stands for "PHP Hypertext Preprocessor”
– An embedded scripting language for HTML like
ASP or JSP
6
• Conceived in 1994 and developed by Rasmus
Lerdorf
• PHP runs on the web server
• Javascript and Flash in contrast, are clientside because they run on a web client
• The instructions in a PHP program cause the
PHP interpreter on a web server to output a
web page. The instructions in Javascript
cause Firefox to run browser commands.
7
PHP
Server-Side scripting Language
•
PHP is a widely-used general-purpose
scripting language that is especially suited for
Web development and can be embedded into
HTML.
•
The PHP syntax is very similar to Perl and
C. PHP is often used together with Apache
(web server) on various operating systems. It
also supports ISAPI and can be used with
Microsoft's IIS on Windows
8
What's So Great About PHP?
•
•
•
•
•
PHP
PHP
PHP
PHP
PHP
is free
is cross-platform
is widely used
hides its complexity
is built for Web Programming
9
PHP vs ASP
Cost:
• To run ASP programs one needs IIS installed on a Windows
platform server, which is not free. PHP programs run on any OS.
PHP is free. Even the connectivity of the database is expensive
in the case of ASP as MS-SQL is a product of Microsoft that
needs to be purchased. PHP generally uses MySQL, which is
freely available.
Speed:
• If we compare the speed of ASP and PHP then PHP has an
upper hand. PHP code runs faster than ASP. ASP is built on COM
based architecture, which is an overhead for the server whereas
PHP code runs in its own memory space.
10
PHP vs ASP
• Platform Compatibility:
PHP programs can run on various platforms like Linux, Unix,
Windows and Solaris whereas ASP is mainly associated with
Windows platforms. However, ASP can run on a Linux platform
with ASP-Apache installed on the server.
• Additional Costs:
Many of the tools used in PHP are free of cost and since PHP is
open source a lot of code can be found in open source forums.
PHP has inbuilt features like ftp, email from a web page or even
encryption mechanisms but in ASP such features are not built in
and some additional components are required. Therefore an
additional cost is incurred for such components.
11
Advantages of PHP
•
•
•
•
•
•
•
•
Light-weight
Cross-platform compatible
Procedural / Object Oriented programming
Supports wide range of Databases
Supports wide variety of outputs
Supports wide variety of protocols
Extremely useful text processing features
Open source
12
13
There are currently about 21
million domains running/using
PHP
(Market share is about 35% for PHP, ASP 21% and
43% websites use non or other tools)
Source: Netcraft)
14
• Performance*
– Zdnet Statistics
• PHP pumped out about 47 pages/second
• Microsoft ASP pumped out about 43
pages/second
• Allaire ColdFusion pumped out about 29
pages/second
• Sun Java JSP pumped out about 13
pages/second
* From PHP HOWTO, July 2001
15
Server-side execution using PHP Script
Web-Client
HTML-Form
Web-Server
Call PHP
interpreter
Submit
Form Data
Web-Browser
WWW
Response
PHP
Script
Response
Reply
16
Putting it all together
Database
Server
Web-Client
Web-Server
HTML-Form
(+JavaScript)
Web-Browser
WWW
Response
Reply
Call PHP
interpreter
Submit
Data
PHP
Script
Response
DBMS
LAN
SQL
commands
Database
Output
17
PHP Tags
1) <? echo “msg”; ?>
2) <?php echo “msg”; ?>
3) <script language=”php”>
echo “msg”;
</script>
4) <% echo “msg”; %>
18
• Hello World!: An Example
– Like Perl, there is more than one way to do
it
• <?php echo “Hello World!”; ?>
• <?php
$greeting = “Hello World!”
printf(“%s”, $greeting);
php?>
19
Comments
// The comment
# The comment
/* Multiline comment */
20
PHP Language Basics
Data Types
PHP supports many different data types,
but they are generally divided in two categories:
scalar and composite
Scalar:
boolean
- A value that can only either be true or false
int
- A signed numeric integer value
float
- A signed floating-point value
string
- A collection of binary data
21
Data Types
Compound Data Types:
• PHP supports two compound data types-so called
because they are essentially containers of other data:
• Arrays are containers of ordered data elements; an
array can be used to store and retrieve any other
data type, including numbers, Boolean values,
strings, objects and even other arrays.
• Objects are containers of both data and code. They
form the basis of Object Oriented Programming, and
are also discussed in a separate chapter called Object
Oriented Programming in PHP.
22
Types of Errors
• Compile-time errors
Errors detected by the parser while it is compiling a
script. Cannot be trapped from within the script itself.
• Fatal errors
Errors that halt the execution of a script. Cannot be
trapped.
• Recoverable errors
Errors that represent significant failures, but can still
be handled in a safe way.
23
Types of Errors ...
Types of Errors
Warnings
Recoverable errors that indicate a run-time fault. Do not halt
the execution of the script.
Notices
Indicate that an error condition occurred, but is not
necessarily significant. Do not halt the execution of the script.
24
Variables
• A variable is an identifier prefaced by $
• Example:
$x = 1;
$y = 3.4;
$z = $x + $y;
$a = true;
$s = "hello!";
print ($z . " " . $a . " " . $s);
Output: 4.4 1 hello!
Note: true = non zero or not empty. False = 0 or the empty
string “”
Common novice mistake: Forgetting the $
25
require(),include()
• The require() & include() statement includes
and evaluates the specific file. If the file
doesnt exist require() will produce fatal error
but include() gives a warning.
• require_once() include_once()
• If the file is already included then it will be
ignored.
26
Built-in Functions
– Array Manipulator Functions
• sort, merge, push, pop, slice, splice, keys, count
– COM functions: Interface to Windows COM objects
– Date and Time Functions
• getdate, mkdate, date, gettimeofday, localtime,
strtotime, time
27
Built-in Functions
– Directory Functions
• Platform independent
– Error Handling Functions
• Recover from warnings and errors
– Filesystem Functions
• Access flat files
• Check directory, link, and file status information
• Copy, delete, and rename files
28
Built-in Functions
– Mail Functions
• mail($recipient, $subject, $message)
29
Built-in Functions
– Database Functions
• dba: dbm-style abstraction layer
• dBase
• Frontbase
• Informix
• Ingres II
• Interbase
• mSQL
30
Built-in Functions
– Database Functions (cont.)
• MySQL
• Oracle
• PostgreSQL
• SQL Server
– MING
• Macromedia Flash
– PDF
• Create/manipulate PDF files dynamically
31
Cloning
clone() method
- Offers a way to create a copy of an
object.
32
Access modifiers
• Access modifiers added for properties
and methods
• The three P’s:
– Public: All scopes have access
– Private: Only local object has access
– Protected: Only local and derived objects
have access
33
Tricks and Tips
• Development Tools
– Color coding editors
• vim, Emacs, Visual SlickEdit
– IDEs
• Windows
– Macromedia Dreamweaver
– Allaire Homesite
– Zend’s PHPEdit
• Linux
– Vi
– Vim
34
Database Connectivity
• Connection:
$con =
mysql_connection('host','user','password');
• Select a Database:
$db = mysql_select_db('db',$con);
• Execute a query:
$q = mysql_query('Query');
35
MySQL Functions
• mysql_affected_rows ($link)
-Get number of affected rows in previous MySQL
operation
• mysql_close($link)
-close mysql connection
• mysql_free_result($result)
- Free result memory
• mysql_data_seek($result,int $row_number)
-Move internal result pointer
36
Cookies
• A cookie is a piece of information sent
to a browser by a Web Server.
• The browser then returns that
information to the Web server.
• This is how some Web pages
"remember" your previous visits;
Cookies can also store user preference
information, log-in data, etc
37
Cookies
• PHP transparently supports HTTP
cookies.
• Cookies are a mechanism for storing
data in the remote browser and thus
tracking or identifying return users.
• You can set cookies using the
setcookie() or setrawcookie()
function.
• Cookies are part of the HTTP header, so
setcookie() must be called before any
output is sent to the browser.
38
Cookies
• Create a cookie with setcookie
setcookie(cookie_name, cookie_value,
lifetime)
Example :
setcookie("voted", "true", time() +
86400);
39
Session Tracking
- For session tracking, PHP creates and maintains
a session tracking id
- Create the id with a call to session_start with no
parameters
- Subsequent calls to session_start retrieves any
session variables that were previously
registered in the session
- To create a session variable, use
session_register
40
Session Tracking
• A PHP session variable is used
to store information about, or
change settings for a user
session.
• Session variables hold
information about one single
user, and are available to all
pages in one application.
41
Session Tracking
Start a Session:
Before storing user information in PHP session,
mustfirst start up the session.
Note: The session_start() function must appear
BEFORthe <html> tag:
<?php session_start(); ?>
<html>
<body>
</body>
</html>
42
PHP 5
• Features
– Complete objects
• Objects with constructors
• Abstract classes
• Private, protected and abstract functions
• Private, protected and constant variables
• Namespaces
• Exception handling with try/catch blocks
43
PHP & Web 2.0
44
PHP == Web Integration
45
PHP Ajax Projects
Amodules3, AJASON, AjaxAC,
Cajax, HTS, jPOP, Stratos
Framework, PAJAX, PAJAJ, Flexible
Ajax, Tiny Ajax, SimpleJax,
phpwebbuilder, SAJAX, sniPEAR
46
THANK YOU
47