PHP is a language that is intended to be embedded within an HTML page.
Mostly, the page will be normal HTML, but bits of PHP cote can be
included. When the web server sees a request for a page named
ending with .php, it
passes it through the "PHP
Hypertext Preprocessor" (that's the name
of the language!), sends on the regular html, but strips out the
PHP statements, and does what they say, perhaps printing
some special text at that point.
<?php .....
?>
You must run PHP scripts using
the web server -- http://osiris....
otherwise you will just see the php code, not its
result!
Parse error: syntax error, unexpected
T_VARIABLE in /home/jensen/public_html/phperr.php on line
4
print "hello $fuzzy world";
Notice: Undefined variable: fuzzy in /home/jensen/public_html/phperr.php
on line 4
hello world
error_reporting(E_ALL ^ E_NOTICE); // will show all messages except notices --OR
error_reporting(0); // suppresses all messages -- include this when finished
Alternatively, you can test for an undefined variable with
isset():
if (isset($fuzzy)) // balance the (( ))'s
print "hello $fuzzy world"; //might not print anything
Include comments in your script, to explain what you are doing. Comments start with // and continue to the end of the line. See above examples.
<input name="name">Then $_REQUEST['foo'] will itself be an array, and you can go through it with foreach This is a loop statement, which does the following print statement once for each element of the inner array.
<select name="foo[]" multiple>
$name = $_REQUEST['name'];
print "Hello, $name <br>";
foreach ($_REQUEST['foo'] as $item)
print " You selected $item<br>";
if ($name == "")
die("Please go back and fill in your name");
$vh = htmlspecialchars($value);
// '&','<','>' and '"' are encoded to print properly
$vh = nl2br($vh);
// adds <br> to show newlines (may be in a textarea)
A function takes some number of arguments (values passed in to it), performs
some actions using the arguments, and generally returns a result.
A function is recognized as some name, followed by a pair of
(parentheses), which enclose the arguments. A function generally
returns a single result. The result can be ignored, stored in a
variable, or used in another statement, such as printing it, or as
an argument of another function. For example, printing in html
could be done with:
print nl2br( htmlspecialchars($value) );
// echo a value formatted for html display, including line breaks
Once you know the name of a function, it is easy to find out the details. Just type the name in the search area of the PHP website, et voila! So I will now give examples, without a full description. If you don't know the name, the manual groups them into categories, such as "File functions", look in these for lists with brief descriptions.
As an example of writing your own function, I have devised a
function for you to use, that adds html text to a variable, so you
can call it several times, then use the resulting (long) string,
perhaps more than once. See the add_html(&string, string)
description.
touch myguestbook.txtAlternatively, you can use Places | Osiris -> File | Create | Empty file and then, for that file, Properties | Permissions, others can read and write.
chmod a+rw myguestbook.txt
$guest = fopen ("myguestbook.txt","a"); //open for appending
fwrite($guest, "Hello world\n");
fwrite($guest, "How's your $global warming?\n");
fclose ($guest); // all done, save the file
// can't fwrite any more
In case you want to print (or fwrite) the date and time, use the
date() function. The
argument gives the "format," each letter stands for some element,
such as 'm' for month (as a number). Try this:
print date("D, d M Y -- H:i:s\n");
$ok = mail ($email,"Welcome to my class",$message,
"From: Your Name<yname06@ubishops.ca>\nReply-To: yin@yahoo.ca");
Please do not sent email to anyone other than yourself, nor to a non-existant address. Do not use an <input> to determine the address.
PHP has a set of functions for each kind of database. We have
PostgreSQL on osiris, so use the functions whose names start with
pg_
. If you should change databases, to MySQL, say,
you would have to replace all pg_
with mysql_
.
You connect to a database (on osiris), do any number of 'queries', look at the results of select queries, and close the connection (or else it is closed at the end of the script.) Here is a commented example:
$conn=pg_connect ('dbname=timetable') or die ('Connect failed ');You will note the 'die' syntax if anything goes wrong. This will show you the error message from postgres.
$query = "SELECT givenname, familyname FROM students"
$result = pg_query($query) or die("query failed".pg_last_error());
// print results, similar to next query ...
$query1 ='SELECT * FROM courses where prof=$1'; // will come from form
$result = pg_query_params($query, array($profname))
or die("query failed".pg_last_error());
print pg_num_rows($result) . " rows found for $profname<br>\n";
while ( $line = pg_fetch_assoc($result) )
foreach ($line as $key => $value)
print "$key = ".htmlentities($value, ENT_COMPAT, 'UTF-8')."<br>\n";
/* Free resultset */
pg_free_result($result);
$query2 = 'INSERT into students (given, family, linux) values ($1, $2, $3)';
$result = pg_query_params($query2, array($g, $f, $linux))
or die("query failed".pg_last_error());
// (could fail on duplicate primary key )
print pg_affected_rows ($result) . " rows inserted for $linux<br>\n";
// note: use this, not num_rows, for insert, update, delete queries
pg_close($conn);
In the above example, pg_query_params
does 3 things. It safely escapes any apostrophes (') in the
data, puts single-quotes (') around each value, and then puts the
results in place of $1, $2, etc. The last arguement must be an
array with the same number of elements as there are placeholders.