Title: PHP Introduction
1PHP Introduction
- After this lecture, you should be able to
- Know the fundamental concepts of Web Scripting
Languages in general, PHP in particular. - Make simple scripts with PHP
- PHP Syntax
- PHP Data Types
PHP Intro
2Processing an HTTP Request
1
Client Browser
Apache Web Server
4
2
3
PHP Module
3Web Scripting Languages
- Languages used to create Web pages dynamically,
- e.g., by using data retrieved from a database
- CGI Scripts and Programs
- Perl Practical Extraction and Report Language
- PHP Hypertext Preprocessor
- ASP Active Server Pages
- JSP Java Server Pages
- ASP.NET Visual Basic, C
- Python
4What is PHP?
- PHP is a scripting language used mostly
- for creating web pages dynamically.
- PHP Hypertext Preprocessor. Originally called
Personal Home Page. - PHP is server-side scripting.
- PHP statements can be embedded within an ordinary
HTML page with lt?php . . . ?gt tags. - PHP is dynamically typed.
- PHP code is interpreted.
5Simple PHP Script
lt? x 2 y 3 z x y echo 2
3 z ?gt
Output 2 3 5
6Simple PHP Web Script
lthtmlgt ltheadgt lttitlegt Hello World!
lt/titlegt lt/headgt ltbodygt lt? echo Hello
World!\n ?gt lt/bodygt lt/htmlgt
- Output
- lthtmlgt
- ltheadgt
- lttitlegt
- Hello World!
- lt/titlegt
- lt/headgt
- ltbodygt
- Hello World!
- lt/bodygt
- lt/htmlgt
7PHP Syntax
- Similar to C / Java
- Semi-colon is a statement terminator.
- Variable names start with .
- Operators and expressions are similar to those
used by C and Java with a few additions. - Control structures
- if-else, switch, while, do-while, for,
foreach, - break, continue, . . .
8PHP Data types
- Scalar types
- - boolean
- - integer
- - float (double)
- - string
- Compound types
- - array
- - object
- Special types
- - resource
- - NULL
9Variables Are Not Typed
Since a variable is not typed, a value of any
type can be assigned to it.
A 1 A abc
The type of a datum can be boolean, integer,
float, string, array, object, and resources.
NULL also can be assigned.
10Polymorphism Automatic Data Type Conversion
A 1 B 2 C A B D A .
B echo "C\n" echo "D\n" echo
C\n
Output 3 12 C\n
11Boolean Values
- echo true
- echo false
- echo 1
- echo 0
- echo true ? A B
- echo 1 ? A B
- echo 100 ? A B
- echo xx ? A B
- echo false ? A B
- echo 0 ? A B
- echo ? A B
1 (empty) 1 0 A A A A B B B
12Idiosyncrasies
- echo dog 0
- echo 0 0
- echo 0 false
- echo 0 0
- echo 0 false
- echo 100 100
- echo 100 100
- echo (int) 100
- echo (int) 100xxx
- echo (int) xxx100
1 1 1 1 (empty) 1 (empty) 100 100 0
13Evaluating a Variable within Text Mode
Evaluation lt? x 100 echo \x1 .
x echo \x2 x\n ?gt x3 lt? x ?gt
Evaluation x1 100 x2 100 x3 100
14Special Characters within
- The following special characters can be used
within a string quoted by - \ -- double quote
- \n -- newline
- \r -- carriage return
- \t -- tab
- \\ -- backslash
- \ -- dollar sign
- \, \), \, \
15Parameter Passing to a Function
function add(x, y) x y
return x y
a 10 b 20 echo add(a, b) . \n echo
a . \n echo b . \n
32 10 21
16Including a File
- An external file can be loaded with one of the
following statements - lt? include file path ?gt
- The execution continues even when the file
to be - loaded does not exist.
- lt? require file path ?gt
- The execution terminates when the file to
be - loaded does not exist.
- lt? include_once file path ?gt and
- lt? require_once file path ?gt
- A file is included only once even if it is
requested - multiple times.
17PHP Resources
- PHP Website - http//www.php.net
- PHP Manual Getting Started http//www.php.net/m
anual/en/getting-started.php - Hugh E. Williams and David Lane. Web Database
Applications with PHP MySQL. O'Reilly, ISBN
0-596-00041-3 (optional) - Other resources on the web