Title: Introduction to PHP
1Introduction to PHP
- Basic principles and syntax
2What is PHP?
- PHP Hypertext Preprocessor
- Open-source, server-side scripting language
- Used to generate dynamic web-pages
- PHP scripts reside between reserved PHP tags
- This allows the programmer to embed PHP scripts
within HTML pages
3What is PHP (contd)
- Interpreted language, scripts are parsed at
run-time rather than compiled beforehand - Executed on the server-side
- Source-code not visible by client
- View Source in browsers does not display the
PHP code - Plethora of built-in functions allow for fast
development - Compatible with many popular databases
4What does PHP code look like?
- Structurally similar to C/C
- Supports procedural and object-oriented paradigm
(to some degree) - All PHP statements end with a semi-colon
- Each PHP script must be enclosed in the reserved
PHP tag
lt?php ?gt
5Comments in PHP
- Standard C, C, and shell comment symbols
// C and Java-style comment Shell-style
comments / C-style comments These can span
multiple lines /
6Variables in PHP
- PHP variables must begin with a sign
- Case-sensitive (Foo ! foo ! fOo)
- Global and locally-scoped variables
- Global variables can be used anywher
- Local variables restricted to a function or class
- Certain variable names reserved by PHP
- Form variables (_POST, _GET)
- Server variables (_SERVER)
- Etc.
7Variable usage
lt?php foo 25 // Numerical variablebar
Hello // String variable foo (foo
7) // Multiplies foo by 7bar (bar 7) //
Invalid expression ?gt
8Echo
- The PHP command echo is used to output the
parameters passed to it - The typical usage for this is to send data to the
clients web-browser - Syntax
- void echo (string arg1 , string argn...)
- In practice, arguments are not passed in
parentheses since echo is a language construct
rather than an actual function
9Echo example
lt?php foo 25 // Numerical variablebar
Hello // String variable echo bar //
Outputs Hello echo foo,bar // Outputs
25Hello echo 5x5,foo // Outputs 5x525 echo
5x5foo // Outputs 5x525echo 5x5foo //
Outputs 5x5foo ?gt
- Notice how echo 5x5foo outputs foo rather
than replacing it with 25 - Strings in single quotes ( ) are not
interpreted or evaluated by PHP - This is true for both variables and character
escape-sequences (such as \n or \\)
10Functions
- Functions MUST be defined before then can be
called - Function headers are of the format
- Note that no return type is specified
- Unlike variables, function names are not case
sensitive (foo() Foo() FoO())
function functionName(arg_1, arg_2, , arg_n)
11Functions example
lt?php // This is a function function
foo(arg_1, arg_2) arg_2 arg_1
arg_2 return arg_2 result_1 foo(12,
3) // Store the function echo result_1 //
Outputs 36 echo foo(12, 3) // Outputs 36 ?gt
12The Big Picture for Assignment 2
- Learn about web-servers
- Learn about Apache
- Download / Installation
- Learn about PHP
- Download / Installation
- Configure Apache
- Modify and save the configuration file, then
restart - Learn about file I/O without the aid of SQL
- Design, write, and test scripts to solve
Assignment 2
13Saving Data in Text Files
- PHP has built in functions for File I/O
processing - fopen (..), fwrite(..), fclose(..), fflush(..),
file_get_contents(..) - Using these pre-made functions, File I/O in PHP
is similar to that of C - General Flow
- Open file
- Read data
- Modify data
- Write data
- Close file
14Saving Data in Text Files Example
lt?phpfilename 'test.txt' / Filename for
writing. This is assumed to be in the same
directory as the script /somecontent
"Add this to the file\n" // String to append
to the file// Let's make sure the file exists
and is writable first.if (is_writable(filename))
// Open the file in append mode ( a ) so
that the string being stored is written at the
end of the // file rather than replacing the
existing text if (!handle fopen(filename,
'a')) echo "Cannot open file
(filename)" exit // Write
somecontent to the opened file. if
(fwrite(handle, somecontent) FALSE)
echo "Cannot write to file (filename)"
exit echo "Success, wrote
(somecontent) to file (filename)"
fclose(handle) else echo "The file
filename is not writable" ?gt
15For more information
- http//www.php.net/manual/en/
- http//www.w3schools.com/php/default.asp
- http//www.zend.com/zend/tut/
16Various Notes on PHP
17Header
- The PHP function header(string)is used to send a
raw HTTP header to the browser - Most useful for redirecting the browser to
another page (after a successful login, for
example) -
lt?php // string storing the destination url
http//www.uottawa.ca / Sends a header telling
the browser to navigate to url /
header(Location . url) ?gt
18IMPORTANT NOTE!
It is absolutely vital that any calls to
header() must be made before any other output is
sent to the browser (either by standard HTML
tags, HTML comments, PHPs echo command, or even
blank lines. Failure to ensure this will result
in an error when your PHP script runs
19Preventing Browser-caching
- The header() function can also be used to
prevent the browser from caching the data sent by
PHP. This forces the browser to refresh the page
everytime the page is loaded.
20Anti-caching example
lt?php// Date in the pastheader("Expires Mon,
26 Jul 1997 050000 GMT")// always
modifiedheader("Last-Modified " . gmdate("D, d
M Y His") . " GMT") // HTTP/1.1header("Cache
-Control no-store, no-cache, must-revalidate")h
eader("Cache-Control post-check0, pre-check0",
false)// HTTP/1.0header("Pragma
no-cache")?gt
21MD5 Hashing
- The PHP Function md5(string) takes a string
parameter and returns the md5 hash of that string
(a 32-bit hexadecimal number) - This function does not encrypt the data sent to
it, it merely returns a hash-value for that data.
lt?php password tomatePotate' if
(md5(password) /MD5 of password
stored in file/) echo The passwords
match! ?gt
22Resources
- PHP function header()http//www.php.net/manual/e
n/function.header.php - PHP function md5()
- http//www.php.net/manual/en/function.md5.php
- RSA MD5 Message Digest Algorithmhttp//www.faqs.o
rg/rfcs/rfc1321
23PHP the Easy way
- A simple way to configure PHP and Apache for
Assignment 2
24Configuration woes
- For various reasons, manually configuring PHP to
work with Apache can present a great deal of
trouble - There exists an elegant software package which is
designed to abstract the configuration process
from web-developers
25EasyPHP
- http//www.easyphp.org/
- Automagically installs and configures Apache,
PHP, and MySQL - Defaults to running from http//localhost/
- Installer
- http//www.easyphp.org/telechargements/dn.php?Fea
syphp1-7 - English Readme files (Extract to EasyPHP
Installation Directory) - http//www.easyphp.org/telechargements/dn.php?Fin
dexUS_1.7