Using PHP – Part 2 THIS WEEK
The PHP web site
Detailed use of forms
Reading and writing files
Object oriented PHP
Adding PHP to a web page
Variables, constants, types
Printing strings and variables
What data is available to a PHP page?
How do pages know what a user has typed into a form?
Write a web form
with a text box
Users type PHP code
into the text box
When they hit RUN,
the page executes
the code and shows
You can do this too This is some HTML code
echo "divThis bit is
coded using PHP/div";
And this is HTML again
It was true.
It was false.
// This is a comment.
shown in 3
This is also a comment.
This is a multi-line
float (= double)
Two values, True and False
Case insensitive: True, TRUE, true are all the same.
The following are all considered to be false:
integer 0, float 0.0
empty string "", the string "0"
an empty array, and objects with no members
the special type NULL
Everything else is regarded as true
PHP doesn't support unsigned integers
The maximum values are platform dependent
Real numbers (various formats can be used)
1.234, 1.2e3, 7E-10
'not expanded', "expanded"
can also use HEREDOC format (see manual) Arrays
a = array(value, …); a = array(key = value, …);
To refer to a value, either of these works
▪ datakey, datakey
▪ To see the contents of an array, use print_r(a);
You can define classes in PHP (see manual) Resources
special variables holding references to external
these are used when extracting data from
variables with no value (the only value they can be
assigned is also called NULL, null, Null, etc)
unset(a) makes a equal to NULL Variables begin with a dollar sign
The basic operations available are
much the same as in Java, e.g.
addition x = y + z;
Variables have the same type as
The type of a variable can change
Assignments x = 1;
myname = "Mike";
result = array("hi" = "there);
Constants Normal method
Version 5.3.0 onwards
const NAME = "Mike";
Can only take scalar values
if (cond) … elseif … else …
while (cond) …
do … while (cond)
for (init; step; cond) …
foreach (arr as val) …
foreach (arr as key = val) …
switch (val) case 'x': …; break;
Define functions using function hello()
the function keyword echo "hello there :)";
Can define functions
inside if statements if (x)
function doStuff() …
Can even define
functions inside other
function there() …
PHP 5 includes an function inverse(x)
exception-style if (x)
mechanism for throw new Exception(
handling errors "Can't divide by zero"
else return 1/x;
catch (Exception e) … Suppose you are asked to write a very simple
form-based web page
The user types their name and then submits the
A new page opens, saying hello
For example, if I type "mike" in the box, the new
page says "Hello mike"
If the box is left empty, the new page says "Hello
How do we do this? getname.php sayhello.php
This page contains the form. This page is called by the form
The user submits the form by when the use clicks the submit
clicking a "submit" button. button. It displays the "hello"
Later we'll see how to do both jobs
using just a single page.